The VisAO Camera
profiler.cpp
1 
2 #include "profiler.h"
3 
4 namespace VisAO
5 {
6 
7 profiler::profiler(int nlogs)
8 {
9  logs = 0;
10 
11  set_numlogs(nlogs);
12 
13  fd = 0;
14  logs_per_file = 10000;
15 
16  pthread_mutex_init(&my_mutex, NULL);
17 
18  pthread_mutexattr_t attr;
19  pthread_mutexattr_init(&attr);
20  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
21 
22  pthread_mutex_init(&thmutex, &attr);
23 
24 
25  pthread_cond_init(&thcond, NULL);
26 
27 }
28 
29 int profiler::set_numlogs(int nlogs)
30 {
31  if(logs) delete[] logs;
32 
33  numlogs = nlogs;
34  logs = new seqlog[numlogs];
35  currlog = -1;
36  abscurrlog = -1;
37  abslast_written = -1;
38  last_written = 0;
39 
40  return 0;
41 }
42 
43 int profiler::set_base_path(const std::string& bp)
44 {
45  base_path = bp;
46  return 0;
47 }
48 
49 int profiler::get_nextlog(int clog)
50 {
51  if(clog == numlogs - 1) return 0;
52  else return clog+1;
53 }
54 
55 int profiler::logseqmsg(const char * sm, const char * sp, timespec *st)
56 {
57  //if(pthread_mutex_trylock(&my_mutex) < 0) return -1;
58 
59  currlog = get_nextlog(currlog);
60  abscurrlog++;
61 
62  memcpy(&(logs[currlog].seqmsg), sm, SEQ_MSG_LEN);
63  memcpy(logs[currlog].seqpt, sp, 4);
64  logs[currlog].seqtm.tv_sec = st->tv_sec;
65  logs[currlog].seqtm.tv_nsec = st->tv_nsec;
66 
67  //pthread_mutex_unlock(&my_mutex);
68 
69  return 0;
70 }
71 
72 bool profiler::logs_to_write()
73 {
74  if(abslast_written < abscurrlog) return true;
75  else return false;
76 }
77 
78 int profiler::write_logs()
79 {
80  while(abslast_written < abscurrlog && fd >= 0)
81  {
82  if(fd == 0)
83  {
84  get_visao_filename(fnamebuff, &(logs[last_written].seqtm) );
85  curr_path = base_path + "_" + fnamebuff + ".prof";
86  fd = open(curr_path.c_str(), O_WRONLY | O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
87 
88  if(fd <= 0)
89  {
90  std::cerr << "Profiler error opening file: " << curr_path << "\n";
91  fd = 0;
92  return -1;
93  }
94 
95  logs_in_file =0;
96  }
97 
98  /********** For Testing *****************
99  char app[3];
100  char spt[5];
101  uint32_t sn;
102  double st;
103 
104  parse_seqmsg(app, &sn, (logs[last_written].seqmsg));
105  app[2] = '\0';
106  memcpy(spt, logs[last_written].seqpt, 4);
107  spt[4] = '\0';
108 
109  st = ((double)logs[last_written].seqtm.tv_sec+((double)logs[last_written].seqtm.tv_nsec/1e9));
110 
111  //std::cout.precision(20);
112  //std::cout << "Profiler: " << last_written << " " << app << " " << sn << " " << spt << " " << st << "\n";
113  */
114  /***************************************/
115 
116  write(fd, &(logs[last_written]), sizeof(seqlog));
117  logs_in_file++;
118  last_written = get_nextlog(last_written);
119  abslast_written++;
120 
122  {
123  close(fd);
124  fd = 0;
125  }
126  }
127  return 0;
128 }
129 
130 
131 void profiler::start()
132 {
133  //signal(SIGIO, SIG_IGN);
134 
135  sigset_t set;
136  sigemptyset(&set);
137  sigaddset(&set, SIGIO);
138 
139  pthread_sigmask(SIG_BLOCK, &set, 0);
140 
141  std::cout << "profiler started\n";
142  //LOG_INFO("profiler started\n");
143  while(1)
144  {
145  pthread_mutex_lock(&thmutex);
146  pthread_cond_wait(&thcond, &thmutex);
147  pthread_mutex_unlock(&thmutex);
148  write_logs();
149  }
150  return;
151 }
152 
153 
154 }//namespace VisAO
155 
int logs_per_file
maximum number of logs to write per file.
Definition: profiler.h:45
int abslast_written
last file written
Definition: profiler.h:49
int last_written
index of the last file written.
Definition: profiler.h:48
int logs_in_file
number of logs in current file.
Definition: profiler.h:46
int get_visao_filename(char *buffer, struct timeval *tv)
Writes the standard visao unique filename for a timeval to the buffer.
The namespace of VisAO software.
int fd
file descriptor of the currently open file.
Definition: profiler.h:43