source: abuse/tags/pd/macabuse/src/profile.c @ 475

Last change on this file since 475 was 49, checked in by Sam Hocevar, 15 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1#include "profile.hpp"
2#include "jwindow.hpp"
3#include "property.hpp"
4#include "objects.hpp"
5
6extern window_manager *eh;
7
8
9jwindow *prof_win=NULL;
10int prof_height=10;
11
12struct prof_info
13{
14  ushort otype;
15  float total_time;
16};
17
18
19prof_info *prof_list=NULL;
20
21
22int profiling() { return prof_list!=NULL; }
23
24void profile_toggle()
25{
26  if (prof_win) { profile_uninit(); }
27  else profile_init();
28}
29
30int 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
39void 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
55void 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
67void 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
75void profile_add_time(int type, float amount)
76{
77  if (prof_list)
78  { prof_list[type].total_time+=amount; }
79}
80
81static 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
90static void profile_sort()
91{
92  qsort(prof_list,total_objects,sizeof(prof_info),p_sorter);
93}
94
95
96void 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        char s[280];
111  for (;i<prof_height;i++)
112  {
113#if 1
114    console_font->put_string(prof_win->screen,spliter+1,dy,object_names[prof_list[i].otype]);
115    prof_win->screen->bar(spliter-1-(int)(prof_list[i].total_time*time_scaler),dy+1,
116                          spliter-1,
117                          dy+console_font->height()-1,eh->bright_color());
118#else
119                sprintf(s, "%8f:%s", prof_list[i].total_time*1000.0, object_names[prof_list[i].otype]);
120    console_font->put_string(prof_win->screen,0,dy,s);
121#endif
122    dy+=console_font->height()+1;
123  }
124
125}
126
127
128
129
130
131
Note: See TracBrowser for help on using the repository browser.