0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-28 01:04:41 +08:00

removed condition variable waiting because all threads were aligned into one chain, waiting each other.

* modified sample functions: added heavy memory allocation operations.
This commit is contained in:
Victor Zarubkin 2016-06-26 12:47:39 +03:00
parent d997a48989
commit c5b7399eab

View File

@ -11,6 +11,8 @@ std::condition_variable cv;
std::mutex cv_m; std::mutex cv_m;
int g_i = 0; int g_i = 0;
const int OBJECTS = 9000;
void loadingResources(){ void loadingResources(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Lightcyan); PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Lightcyan);
std::this_thread::sleep_for(std::chrono::milliseconds(50)); std::this_thread::sleep_for(std::chrono::milliseconds(50));
@ -18,29 +20,42 @@ void loadingResources(){
void prepareMath(){ void prepareMath(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue); PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue);
volatile int i = 0; int* intarray = new int[OBJECTS];
for (; i < 100000; ++i); for (int i = 0; i < OBJECTS; ++i)
intarray[i] = i * i;
delete[] intarray;
//std::this_thread::sleep_for(std::chrono::milliseconds(3)); //std::this_thread::sleep_for(std::chrono::milliseconds(3));
} }
void calcIntersect(){ void calcIntersect(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue); PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue);
volatile int i = 0; //int* intarray = new int[OBJECTS * OBJECTS];
for (; i < 500000; ++i); int* intarray = new int[OBJECTS];
for (int i = 0; i < OBJECTS; ++i)
{
for (int j = i; j < OBJECTS; ++j)
//intarray[i * OBJECTS + j] = i * j - i / 2 + (OBJECTS - j) * 5;
intarray[j] = i * j - i / 2 + (OBJECTS - j) * 5;
}
delete[] intarray;
//std::this_thread::sleep_for(std::chrono::milliseconds(4)); //std::this_thread::sleep_for(std::chrono::milliseconds(4));
} }
void calcPhys(){ void calcPhys(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue); PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue);
volatile int i = 0; int* intarray = new int[OBJECTS];
for (; i < 400000; ++i); for (int i = 0; i < OBJECTS; ++i)
intarray[i] = i * i + i / 3 - (OBJECTS - i) / 2;
calcIntersect(); calcIntersect();
delete[] intarray;
} }
void calcBrain(){ void calcBrain(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue); PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue);
volatile int i = 0; int* intarray = new int[OBJECTS];
for (; i < 300000; ++i); for (int i = 0; i < OBJECTS; ++i)
intarray[i] = i * i * i - i / 10 + (OBJECTS - i) * 7 + i * 180 / 3;
delete[] intarray;
//std::this_thread::sleep_for(std::chrono::milliseconds(3)); //std::this_thread::sleep_for(std::chrono::milliseconds(3));
} }
@ -48,30 +63,28 @@ void calculateBehavior(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Lightblue); PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Lightblue);
calcPhys(); calcPhys();
calcBrain(); calcBrain();
volatile int i = 0;
for (; i < 150000; ++i);
} }
void modellingStep(){ void modellingStep(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Navy); PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Navy);
prepareMath(); prepareMath();
calculateBehavior(); calculateBehavior();
volatile int i = 0;
for (; i < 100000; ++i);
} }
void prepareRender(){ void prepareRender(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Lightred); PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Lightred);
volatile int i = 0; volatile int i = 0;
for (; i < 350000; ++i); for (; i < 200000; ++i);
//std::this_thread::sleep_for(std::chrono::milliseconds(8)); //std::this_thread::sleep_for(std::chrono::milliseconds(8));
} }
void calculatePhysics(){ void calculatePhysics(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Red); PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Red);
volatile int i = 0; unsigned int* intarray = new unsigned int[OBJECTS];
for (; i < 400000; ++i); for (unsigned int i = 0; i < OBJECTS; ++i)
intarray[i] = i * i * i * i / 100 + i / 3 - (OBJECTS - i) * 15;
delete[] intarray;
//std::this_thread::sleep_for(std::chrono::milliseconds(8)); //std::this_thread::sleep_for(std::chrono::milliseconds(8));
} }
@ -79,13 +92,11 @@ void frame(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Magenta); PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Magenta);
prepareRender(); prepareRender();
calculatePhysics(); calculatePhysics();
volatile int i = 0;
for (; i < 30000; ++i);
} }
void loadingResourcesThread(){ void loadingResourcesThread(){
std::unique_lock<std::mutex> lk(cv_m); //std::unique_lock<std::mutex> lk(cv_m);
cv.wait(lk, []{return g_i == 1; }); //cv.wait(lk, []{return g_i == 1; });
for(int i = 0; i < 10; i++){ for(int i = 0; i < 10; i++){
loadingResources(); loadingResources();
PROFILER_ADD_EVENT_GROUPED("Resources Loading!",profiler::colors::Cyan); PROFILER_ADD_EVENT_GROUPED("Resources Loading!",profiler::colors::Cyan);
@ -94,18 +105,20 @@ void loadingResourcesThread(){
} }
void modellingThread(){ void modellingThread(){
std::unique_lock<std::mutex> lk(cv_m); //std::unique_lock<std::mutex> lk(cv_m);
cv.wait(lk, []{return g_i == 1; }); //cv.wait(lk, []{return g_i == 1; });
for (int i = 0; i < 1600; i++){ for (int i = 0; i < 1600; i++){
modellingStep(); modellingStep();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
} }
} }
void renderThread(){ void renderThread(){
std::unique_lock<std::mutex> lk(cv_m); //std::unique_lock<std::mutex> lk(cv_m);
cv.wait(lk, []{return g_i == 1; }); //cv.wait(lk, []{return g_i == 1; });
for (int i = 0; i < 1000; i++){ for (int i = 0; i < 1000; i++){
frame(); frame();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
} }
} }
@ -206,5 +219,9 @@ int main()
int blocks_counter = fillTreesFromFile("test.prof", threaded_trees); int blocks_counter = fillTreesFromFile("test.prof", threaded_trees);
std::cout << "Blocks count: " << blocks_counter << std::endl; std::cout << "Blocks count: " << blocks_counter << std::endl;
char c;
std::cout << "Enter something to exit: ";
std::cin >> c;
return 0; return 0;
} }