main.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //#define FULL_DISABLE_PROFILER
  2. #include <chrono>
  3. #include <thread>
  4. #include <vector>
  5. #include <iostream>
  6. #include <condition_variable>
  7. #include <cstdlib>
  8. #include <math.h>
  9. #include <easy/profiler.h>
  10. #include <easy/reader.h>
  11. std::condition_variable cv;
  12. std::mutex cv_m;
  13. int g_i = 0;
  14. int OBJECTS = 500;
  15. int MODELLING_STEPS = 1500;
  16. int RENDER_STEPS = 1500;
  17. int RESOURCE_LOADING_COUNT = 50;
  18. //#define SAMPLE_NETWORK_TEST
  19. void localSleep(int magic=200000)
  20. {
  21. //PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue);
  22. volatile int i = 0;
  23. for (; i < magic; ++i);
  24. }
  25. void loadingResources(){
  26. EASY_FUNCTION(profiler::colors::DarkCyan);
  27. localSleep();
  28. // std::this_thread::sleep_for(std::chrono::milliseconds(50));
  29. }
  30. void prepareMath(){
  31. EASY_FUNCTION(profiler::colors::Green);
  32. int* intarray = new int[OBJECTS];
  33. for (int i = 0; i < OBJECTS; ++i)
  34. intarray[i] = i * i;
  35. delete[] intarray;
  36. //std::this_thread::sleep_for(std::chrono::milliseconds(3));
  37. }
  38. void calcIntersect(){
  39. EASY_FUNCTION(profiler::colors::Gold);
  40. //int* intarray = new int[OBJECTS * OBJECTS];
  41. int* intarray = new int[OBJECTS];
  42. for (int i = 0; i < OBJECTS; ++i)
  43. {
  44. for (int j = i; j < OBJECTS; ++j)
  45. //intarray[i * OBJECTS + j] = i * j - i / 2 + (OBJECTS - j) * 5;
  46. intarray[j] = i * j - i / 2 + (OBJECTS - j) * 5;
  47. }
  48. delete[] intarray;
  49. //std::this_thread::sleep_for(std::chrono::milliseconds(4));
  50. }
  51. double multModel(double i)
  52. {
  53. EASY_FUNCTION(profiler::colors::PaleGold);
  54. return i * sin(i) * cos(i);
  55. }
  56. void calcPhys(){
  57. EASY_FUNCTION(profiler::colors::Amber);
  58. double* intarray = new double[OBJECTS];
  59. for (int i = 0; i < OBJECTS; ++i)
  60. intarray[i] = multModel(double(i)) + double(i / 3) - double((OBJECTS - i) / 2);
  61. calcIntersect();
  62. delete[] intarray;
  63. }
  64. double calcSubbrain(int i)
  65. {
  66. EASY_FUNCTION(profiler::colors::Navy);
  67. return i * i * i - i / 10 + (OBJECTS - i) * 7 ;
  68. }
  69. void calcBrain(){
  70. EASY_FUNCTION(profiler::colors::LightBlue);
  71. double* intarray = new double[OBJECTS];
  72. for (int i = 0; i < OBJECTS; ++i)
  73. intarray[i] = calcSubbrain(i) + double(i * 180 / 3);
  74. delete[] intarray;
  75. //std::this_thread::sleep_for(std::chrono::milliseconds(3));
  76. }
  77. void calculateBehavior(){
  78. EASY_FUNCTION(profiler::colors::Blue);
  79. calcPhys();
  80. calcBrain();
  81. }
  82. void modellingStep(){
  83. EASY_FUNCTION();
  84. prepareMath();
  85. calculateBehavior();
  86. }
  87. void prepareRender(){
  88. EASY_FUNCTION(profiler::colors::Brick);
  89. localSleep();
  90. //std::this_thread::sleep_for(std::chrono::milliseconds(8));
  91. }
  92. int multPhys(int i)
  93. {
  94. EASY_FUNCTION(profiler::colors::Red700, profiler::ON);
  95. return i * i * i * i / 100;
  96. }
  97. int calcPhysicForObject(int i)
  98. {
  99. EASY_FUNCTION(profiler::colors::Red);
  100. return multPhys(i) + i / 3 - (OBJECTS - i) * 15;
  101. }
  102. void calculatePhysics(){
  103. EASY_FUNCTION(profiler::colors::Red);
  104. unsigned int* intarray = new unsigned int[OBJECTS];
  105. for (int i = 0; i < OBJECTS; ++i)
  106. intarray[i] = calcPhysicForObject(i);
  107. delete[] intarray;
  108. //std::this_thread::sleep_for(std::chrono::milliseconds(8));
  109. }
  110. void frame(){
  111. EASY_FUNCTION(profiler::colors::Magenta);
  112. prepareRender();
  113. calculatePhysics();
  114. }
  115. void loadingResourcesThread(){
  116. //std::unique_lock<std::mutex> lk(cv_m);
  117. //cv.wait(lk, []{return g_i == 1; });
  118. EASY_THREAD("Resource loading");
  119. #ifdef SAMPLE_NETWORK_TEST
  120. while (true) {
  121. #else
  122. for(int i = 0; i < RESOURCE_LOADING_COUNT; i++){
  123. #endif
  124. loadingResources();
  125. EASY_EVENT("Resources Loading!", profiler::colors::Cyan);
  126. localSleep(1200000);
  127. //std::this_thread::sleep_for(std::chrono::milliseconds(20));
  128. }
  129. }
  130. void modellingThread(){
  131. //std::unique_lock<std::mutex> lk(cv_m);
  132. //cv.wait(lk, []{return g_i == 1; });
  133. EASY_THREAD("Modelling");
  134. #ifdef SAMPLE_NETWORK_TEST
  135. while (true) {
  136. #else
  137. for (int i = 0; i < MODELLING_STEPS; i++){
  138. #endif
  139. EASY_END_BLOCK;
  140. EASY_NONSCOPED_BLOCK("Frame");
  141. modellingStep();
  142. localSleep(1200000);
  143. //std::this_thread::sleep_for(std::chrono::milliseconds(20));
  144. }
  145. EASY_END_BLOCK;
  146. }
  147. void renderThread(){
  148. //std::unique_lock<std::mutex> lk(cv_m);
  149. //cv.wait(lk, []{return g_i == 1; });
  150. EASY_THREAD("Render");
  151. #ifdef SAMPLE_NETWORK_TEST
  152. while (true) {
  153. #else
  154. for (int i = 0; i < RENDER_STEPS; i++){
  155. #endif
  156. frame();
  157. localSleep(1200000);
  158. //std::this_thread::sleep_for(std::chrono::milliseconds(20));
  159. }
  160. }
  161. //////////////////////////////////////////////////////////////////////////
  162. int main(int argc, char* argv[])
  163. {
  164. if (argc > 1 && argv[1]){
  165. OBJECTS = std::atoi(argv[1]);
  166. }
  167. if (argc > 2 && argv[2]){
  168. MODELLING_STEPS = std::atoi(argv[2]);
  169. }
  170. if (argc > 3 && argv[3]){
  171. RENDER_STEPS = std::atoi(argv[3]);
  172. }
  173. if (argc > 4 && argv[4]){
  174. RESOURCE_LOADING_COUNT = std::atoi(argv[4]);
  175. }
  176. std::cout << "Objects count: " << OBJECTS << std::endl;
  177. std::cout << "Render steps: " << MODELLING_STEPS << std::endl;
  178. std::cout << "Modelling steps: " << RENDER_STEPS << std::endl;
  179. std::cout << "Resource loading count: " << RESOURCE_LOADING_COUNT << std::endl;
  180. auto start = std::chrono::system_clock::now();
  181. #ifndef SAMPLE_NETWORK_TEST
  182. EASY_PROFILER_ENABLE;
  183. #endif
  184. EASY_MAIN_THREAD;
  185. profiler::startListen();
  186. std::vector<std::thread> threads;
  187. //for (int i=0; i < 3; i++)
  188. {
  189. threads.emplace_back(loadingResourcesThread);
  190. threads.emplace_back(renderThread);
  191. threads.emplace_back(modellingThread);
  192. }
  193. cv_m.lock();
  194. g_i = 1;
  195. cv_m.unlock();
  196. cv.notify_all();
  197. #ifndef SAMPLE_NETWORK_TEST
  198. std::atomic_bool stop = ATOMIC_VAR_INIT(false);
  199. auto frame_time_printer_thread = std::thread([&stop]()
  200. {
  201. while (!stop.load(std::memory_order_acquire))
  202. {
  203. std::cout << "Frame time: max " << profiler::main_thread::frameTimeLocalMax() << " us // avg " << profiler::main_thread::frameTimeLocalAvg() << " us\n";
  204. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  205. }
  206. });
  207. #endif
  208. modellingThread();
  209. #ifndef SAMPLE_NETWORK_TEST
  210. stop.store(true, std::memory_order_release);
  211. frame_time_printer_thread.join();
  212. #endif
  213. for(auto& t : threads)
  214. t.join();
  215. auto end = std::chrono::system_clock::now();
  216. auto elapsed =
  217. std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  218. std::cout << "Elapsed time: " << elapsed.count() << " usec" << std::endl;
  219. auto blocks_count = profiler::dumpBlocksToFile("test.prof");
  220. std::cout << "Blocks count: " << blocks_count << std::endl;
  221. return 0;
  222. }