profiler.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "pocketpy/profiler.h"
  2. namespace pkpy{
  3. static std::string left_pad(std::string s, int width){
  4. int n = width - s.size();
  5. if(n <= 0) return s;
  6. return std::string(n, ' ') + s;
  7. }
  8. static std::string to_string_1f(f64 x){
  9. char buf[32];
  10. snprintf(buf, 32, "%.1f", x);
  11. return buf;
  12. }
  13. void LineProfiler::begin(){
  14. frames.clear();
  15. }
  16. void LineProfiler::_step(FrameId frame){
  17. auto line_info = frame->co->lines[frame->_ip];
  18. if(line_info.is_virtual) return;
  19. std::string_view filename = frame->co->src->filename.sv();
  20. int line = line_info.lineno;
  21. if(frames.empty()){
  22. frames.push({frame, clock(), nullptr});
  23. }else{
  24. _step_end(frame, line);
  25. }
  26. std::vector<_LineRecord>& file_records = records[filename];
  27. if(file_records.empty()){
  28. // initialize file_records
  29. int total_lines = frame->co->src->line_starts.size();
  30. file_records.resize(total_lines + 1);
  31. for(int i=1; i<=total_lines; i++){
  32. file_records[i].line = i;
  33. }
  34. }
  35. frames.top().prev_record = &file_records.at(line);
  36. }
  37. void LineProfiler::_step_end(FrameId frame, int line){
  38. clock_t now = clock();
  39. _FrameRecord& top_frame_record = frames.top();
  40. _LineRecord* prev_record = top_frame_record.prev_record;
  41. int id_delta = frame.index - top_frame_record.frame.index;
  42. PK_ASSERT(id_delta >= -1 && id_delta <= 1);
  43. // current line is about to change
  44. if(prev_record->line != line){
  45. clock_t delta = now - top_frame_record.prev_time;
  46. top_frame_record.prev_time = now;
  47. if(id_delta != 1) prev_record->hits++;
  48. prev_record->time += delta;
  49. }
  50. if(id_delta == 1){
  51. frames.push({frame, now, nullptr});
  52. }else{
  53. if(id_delta == -1) frames.pop();
  54. }
  55. }
  56. void LineProfiler::end(){
  57. clock_t now = clock();
  58. _FrameRecord& top_frame_record = frames.top();
  59. _LineRecord* prev_record = top_frame_record.prev_record;
  60. clock_t delta = now - top_frame_record.prev_time;
  61. top_frame_record.prev_time = now;
  62. prev_record->hits++;
  63. prev_record->time += delta;
  64. frames.pop();
  65. PK_ASSERT(frames.empty());
  66. }
  67. Str LineProfiler::stats(){
  68. SStream ss;
  69. for(FuncDecl* decl: functions){
  70. int start_line = decl->code->start_line;
  71. int end_line = decl->code->end_line;
  72. if(start_line == -1 || end_line == -1) continue;
  73. std::string_view filename = decl->code->src->filename.sv();
  74. std::vector<_LineRecord>& file_records = records[filename];
  75. if(file_records.empty()) continue;
  76. clock_t total_time = 0;
  77. for(int line = start_line; line <= end_line; line++){
  78. total_time += file_records.at(line).time;
  79. }
  80. ss << "Total time: " << (f64)total_time / CLOCKS_PER_SEC << "s\n";
  81. ss << "File: " << filename << "\n";
  82. ss << "Function: " << decl->code->name << " at line " << start_line << "\n";
  83. ss << "Line # Hits Time Per Hit % Time Line Contents\n";
  84. ss << "==============================================================\n";
  85. for(int line = start_line; line <= end_line; line++){
  86. const _LineRecord& record = file_records.at(line);
  87. if(!record.is_valid()) continue;
  88. ss << left_pad(std::to_string(line), 6);
  89. if(record.hits == 0){
  90. ss << std::string(10 + 13 + 9 + 9, ' ');
  91. }else{
  92. ss << left_pad(std::to_string(record.hits), 10);
  93. ss << left_pad(std::to_string(record.time), 13);
  94. ss << left_pad(std::to_string(record.time / record.hits), 9);
  95. if(total_time == 0){
  96. ss << left_pad("0.0", 9);
  97. }else{
  98. ss << left_pad(to_string_1f(record.time * (f64)100 / total_time), 9);
  99. }
  100. }
  101. // line_content
  102. auto [_0, _1] = decl->code->src->_get_line(line);
  103. ss << " " << std::string_view(_0, _1-_0) << "\n";
  104. }
  105. ss << "\n";
  106. }
  107. return ss.str();
  108. }
  109. } // namespace pkpy