[2] | 1 | #include "profile.hpp" |
---|
| 2 | #include "jwindow.hpp" |
---|
| 3 | #include "property.hpp" |
---|
| 4 | #include "objects.hpp" |
---|
| 5 | |
---|
| 6 | extern window_manager *eh; |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | jwindow *prof_win=NULL; |
---|
| 10 | int prof_height=10; |
---|
| 11 | |
---|
| 12 | struct prof_info |
---|
| 13 | { |
---|
[17] | 14 | uint16_t otype; |
---|
[2] | 15 | float total_time; |
---|
| 16 | }; |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | prof_info *prof_list=NULL; |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | int profiling() { return prof_list!=NULL; } |
---|
| 23 | |
---|
| 24 | void profile_toggle() |
---|
| 25 | { |
---|
| 26 | if (prof_win) { profile_uninit(); } |
---|
| 27 | else profile_init(); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | int profile_handle_event(event &ev) |
---|
| 31 | { |
---|
| 32 | if (ev.type==EV_CLOSE_WINDOW && ev.window==prof_win) |
---|
| 33 | { |
---|
| 34 | profile_toggle(); |
---|
| 35 | return 1; |
---|
| 36 | } else return 0; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | void profile_init() |
---|
| 40 | { |
---|
| 41 | if (prof_list) { profile_uninit(); } |
---|
| 42 | prof_list=(prof_info *)jmalloc(sizeof(prof_info)*total_objects,"prof info"); |
---|
| 43 | profile_reset(); |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | prof_win=eh->new_window(prop->getd("profile x",-1), |
---|
| 47 | prop->getd("profile y",-1), |
---|
| 48 | 20*console_font->width(), |
---|
| 49 | (prof_height+1)*console_font->height(), |
---|
| 50 | NULL, |
---|
| 51 | "PROFILE"); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | void profile_reset() |
---|
| 56 | { |
---|
| 57 | int i; |
---|
| 58 | for (i=0;i<total_objects;i++) |
---|
| 59 | { |
---|
| 60 | prof_list[i].otype=i; |
---|
| 61 | prof_list[i].total_time=0; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | void profile_uninit() |
---|
| 68 | { |
---|
| 69 | if (prof_list) jfree(prof_list); |
---|
| 70 | prof_list=NULL; |
---|
| 71 | if (prof_win) { eh->close_window(prof_win); prof_win=NULL; } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | void profile_add_time(int type, float amount) |
---|
| 76 | { |
---|
| 77 | if (prof_list) |
---|
| 78 | { prof_list[type].total_time+=amount; } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | static int p_sorter(const void *a, const void *b) |
---|
| 82 | { |
---|
| 83 | if (((prof_info *)a)->total_time<((prof_info *)b)->total_time) |
---|
| 84 | return 1; |
---|
| 85 | else if (((prof_info *)a)->total_time>((prof_info *)b)->total_time) |
---|
| 86 | return -1; |
---|
| 87 | else return 0; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | static void profile_sort() |
---|
| 91 | { |
---|
| 92 | qsort(prof_list,total_objects,sizeof(prof_info),p_sorter); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | void profile_update() |
---|
| 97 | { |
---|
| 98 | profile_sort(); |
---|
| 99 | if (prof_list[0].total_time<=0.0) return ; // nothing took any time! |
---|
| 100 | |
---|
| 101 | int i=0; |
---|
| 102 | int spliter=(prof_win->x2()+prof_win->x1())/2; |
---|
| 103 | int max_bar_length=spliter-prof_win->x1(); |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | float time_scaler=(float)max_bar_length/prof_list[0].total_time; |
---|
| 107 | |
---|
| 108 | prof_win->screen->bar(0,prof_win->y1(),prof_win->screen->width()-1,prof_win->screen->height(),0); |
---|
| 109 | int dy=WINDOW_FRAME_TOP; |
---|
| 110 | for (;i<prof_height;i++) |
---|
| 111 | { |
---|
| 112 | console_font->put_string(prof_win->screen,spliter+1,dy,object_names[prof_list[i].otype]); |
---|
| 113 | prof_win->screen->bar(spliter-1-(int)(prof_list[i].total_time*time_scaler),dy+1, |
---|
| 114 | spliter-1, |
---|
| 115 | dy+console_font->height()-1,eh->bright_color()); |
---|
| 116 | dy+=console_font->height()+1; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | |
---|