1 #include "myddas_structs.h"
2 #include "myddas_statistics.h"
3 #include "Yap.h"
4 #include <stdlib.h>
5 #include <sys/time.h>
6
7 #if defined MYDDAS_STATS
8
9
10
11 /* Documentation: Time Units
12 ------------------------------------------------------------------------
13 *****| Second(s) | MiliSeconds(ms) | MicroSeconds(us) | NanoSecond(ns) |
14 -----|-----------|-----------------|------------------|----------------|
15 s | 1 | 0.001 | 0.000001 | 1e-9 |
16 ms | 1000 | 1 | 0.001 | 0.000001 |
17 us | 10000000 | 1000 | 1 | 0.001 |
18 ns |1000000000 | 1000000 | 1000 | 1 |
19 ------------------------------------------------------------------------
20
21 ------
22
23 The struct timeval structure represents an elapsed time. It is
24 declared in `sys/time.h' and has the following members:
25
26 long int tv_sec -> This represents the number of whole seconds of
27 elapsed time.
28
29 long int tv_usec -> This is the rest of the elapsed time (a fraction
30 of a second), represented as the number of microseconds. It is
31 always less than one million.
32
33
34 ------
35
36 The struct timespec structure represents an elapsed time. It is
37 declared in `time.h' and has the following members:
38
39 long int tv_sec -> This represents the number of whole seconds of
40 elapsed time.
41
42 long int tv_nsec -> This is the rest of the elapsed time (a fraction
43 of a second), represented as the number of nanoseconds. It is
44 always less than one billion.
45
46 -----
47
48 The gettimeofday() function shall obtain the current time,
49 expressed as seconds and microseconds since the Epoch, and store
50 it in the timeval structure pointed to by tp. The resolution of
51 the system clock is unspecified.
52
53 If tzp is not a null pointer, the behavior is unspecified.
54
55 */
56
57 static void
58 myddas_stats_time_subtract (unsigned long *, unsigned long *, MYDDAS_STATS_TIME, MYDDAS_STATS_TIME);
59 static void
60 myddas_stats_add_seconds_time(MYDDAS_STATS_TIME,unsigned long, unsigned long);
61 static void
62 myddas_stats_integrity_of_time(MYDDAS_STATS_TIME);
63
64 /* Be shore to delete MYDDAS_STATS_TIME structure */
65 MYDDAS_STATS_TIME
myddas_stats_walltime(void)66 myddas_stats_walltime(void) {
67
68 MYDDAS_STATS_TIME myddas_time = NULL;
69 MYDDAS_MALLOC(myddas_time,struct myddas_stats_time_struct);
70 myddas_time->type = time_copy;
71
72 struct timeval *time = NULL;
73 MYDDAS_MALLOC(time,struct timeval);
74
75 gettimeofday(time,NULL);
76
77 myddas_time->u.time_copy.tv_sec = time->tv_sec;
78 myddas_time->u.time_copy.tv_usec = time->tv_usec;
79
80 MYDDAS_FREE(time,struct timeval);
81
82 return myddas_time;
83 }
84
85 void
myddas_stats_add_time(MYDDAS_STATS_TIME sum,MYDDAS_STATS_TIME time1,MYDDAS_STATS_TIME time2)86 myddas_stats_add_time(MYDDAS_STATS_TIME sum, MYDDAS_STATS_TIME time1,MYDDAS_STATS_TIME time2){
87
88 if (sum->type == time_final){
89 sum->u.time_final.microseconds =
90 time1->u.time_final.microseconds +
91 time2->u.time_final.microseconds;
92 sum->u.time_final.miliseconds =
93 time1->u.time_final.miliseconds +
94 time2->u.time_final.miliseconds;
95 sum->u.time_final.seconds =
96 time1->u.time_final.seconds +
97 time2->u.time_final.seconds;
98 sum->u.time_final.minutes =
99 time1->u.time_final.minutes +
100 time2->u.time_final.minutes;
101 sum->u.time_final.hours =
102 time1->u.time_final.hours +
103 time2->u.time_final.hours;
104 } else {
105 sum->u.time_copy.tv_sec =
106 time1->u.time_copy.tv_sec +
107 time2->u.time_copy.tv_sec;
108 sum->u.time_copy.tv_usec =
109 time1->u.time_copy.tv_usec +
110 time2->u.time_copy.tv_usec;
111 }
112
113 myddas_stats_integrity_of_time(sum);
114 }
115
116
117 void
myddas_stats_subtract_time(MYDDAS_STATS_TIME result,MYDDAS_STATS_TIME t1,MYDDAS_STATS_TIME t2)118 myddas_stats_subtract_time(MYDDAS_STATS_TIME result, MYDDAS_STATS_TIME t1,MYDDAS_STATS_TIME t2){
119
120 if (result->type == time_copy){
121
122 unsigned long sec;
123 unsigned long usec;
124 myddas_stats_time_subtract(&sec,&usec,t1,t2);
125
126 result->u.time_copy.tv_sec = sec;
127 result->u.time_copy.tv_usec = usec;
128
129 } else {
130
131 }
132
133 }
134
135 void
myddas_stats_move_time(MYDDAS_STATS_TIME from,MYDDAS_STATS_TIME to)136 myddas_stats_move_time(MYDDAS_STATS_TIME from,
137 MYDDAS_STATS_TIME to)
138 {
139 if (from->type == time_copy)
140 {
141 to->type = time_copy;
142 to->u.time_copy.tv_sec = from->u.time_copy.tv_sec;
143 to->u.time_copy.tv_usec = from->u.time_copy.tv_usec;
144 }
145 else if (from->type == time_final)
146 {
147 to->u.time_final.hours = from->u.time_final.hours;
148 to->u.time_final.minutes = from->u.time_final.minutes;
149 to->u.time_final.seconds = from->u.time_final.seconds;
150 to->u.time_final.miliseconds = from->u.time_final.miliseconds;
151 to->u.time_final.microseconds = from->u.time_final.microseconds;
152 }
153 MYDDAS_FREE(from,struct myddas_stats_time_struct);
154 }
155
156 MYDDAS_STATS_TIME
myddas_stats_time_copy_to_final(MYDDAS_STATS_TIME t_copy)157 myddas_stats_time_copy_to_final(MYDDAS_STATS_TIME t_copy){
158
159 MYDDAS_STATS_TIME t_final;
160 MYDDAS_STATS_INITIALIZE_TIME_STRUCT(t_final,time_final);
161
162 myddas_stats_add_seconds_time(t_final,
163 t_copy->u.time_copy.tv_sec,
164 t_copy->u.time_copy.tv_usec);
165
166 MYDDAS_FREE(t_copy,struct myddas_stats_time_struct);
167 return t_final;
168 }
169
170 static void
myddas_stats_add_seconds_time(MYDDAS_STATS_TIME myddas_time,unsigned long sec,unsigned long usec)171 myddas_stats_add_seconds_time(MYDDAS_STATS_TIME myddas_time,
172 unsigned long sec,
173 unsigned long usec){
174
175 short hours = sec / 3600;
176 sec %= 3600;
177 short minutes = sec / 60;
178 sec %= 60;
179 short milisec = usec / 1000;
180 usec %= 1000;
181
182 myddas_time->u.time_final.microseconds += usec ;
183 myddas_time->u.time_final.miliseconds += milisec;
184 myddas_time->u.time_final.seconds += sec ;
185 myddas_time->u.time_final.minutes += minutes ;
186 myddas_time->u.time_final.hours += hours;
187
188 myddas_stats_integrity_of_time(myddas_time);
189 }
190
191
192 static void
myddas_stats_time_subtract(unsigned long * sec,unsigned long * usec,MYDDAS_STATS_TIME start,MYDDAS_STATS_TIME end)193 myddas_stats_time_subtract(unsigned long *sec,unsigned long *usec,
194 MYDDAS_STATS_TIME start, MYDDAS_STATS_TIME end){
195
196 /* Perform the carry for the later subtraction by updating y. */
197 if (start->u.time_copy.tv_usec < end->u.time_copy.tv_usec) {
198 int nsec = (end->u.time_copy.tv_usec - start->u.time_copy.tv_usec) / 1000000 + 1;
199 end->u.time_copy.tv_usec -= 1000000 * nsec;
200 end->u.time_copy.tv_sec += nsec;
201 }
202 if (start->u.time_copy.tv_usec - end->u.time_copy.tv_usec > 1000000) {
203 int nsec = (start->u.time_copy.tv_usec - end->u.time_copy.tv_usec) / 1000000;
204 end->u.time_copy.tv_usec += 1000000 * nsec;
205 end->u.time_copy.tv_sec -= nsec;
206 }
207
208 /* Compute the time remaining to wait.
209 tv_usec is certainly positive. */
210 *sec = start->u.time_copy.tv_sec - end->u.time_copy.tv_sec;
211 *usec = start->u.time_copy.tv_usec - end->u.time_copy.tv_usec;
212 }
213
214 static void
myddas_stats_integrity_of_time(MYDDAS_STATS_TIME myddas_time)215 myddas_stats_integrity_of_time(MYDDAS_STATS_TIME myddas_time){
216
217 if (myddas_time->u.time_final.microseconds > 999)
218 {
219 myddas_time->u.time_final.microseconds -= 1000;
220 myddas_time->u.time_final.miliseconds++;
221 }
222 if (myddas_time->u.time_final.miliseconds > 999)
223 {
224 myddas_time->u.time_final.miliseconds -= 1000;
225 myddas_time->u.time_final.seconds++;
226 }
227
228 if (myddas_time->u.time_final.seconds > 59)
229 {
230 myddas_time->u.time_final.seconds -= 60;
231 myddas_time->u.time_final.minutes++;
232 }
233
234 if (myddas_time->u.time_final.minutes > 59)
235 {
236 myddas_time->u.time_final.minutes -= 60;
237 myddas_time->u.time_final.hours++;
238 }
239 }
240
241 MYDDAS_GLOBAL
myddas_stats_initialize_global_stats(MYDDAS_GLOBAL global)242 myddas_stats_initialize_global_stats(MYDDAS_GLOBAL global){
243
244 MYDDAS_STATS_STRUCT stats = NULL;
245
246 short i;
247
248 /* For the time statistics */
249 /*
250 Stats [1] - Total Time spent on the db_row function
251 Stats [2] - Total Time spent on the translate/3 predicate
252 */
253
254 /* First */
255 stats = myddas_stats_initialize_stat(stats,time_str);
256 (global->myddas_statistics)->stats = stats;
257 for(i=0;i<1;i++){
258 myddas_stats_initialize_stat(stats,time_str);
259 }
260
261 return global;
262 }
263
264 MYDDAS_STATS_STRUCT
myddas_stats_initialize_connection_stats()265 myddas_stats_initialize_connection_stats(){
266 /*
267 Stats [1] - Total of Time Spent by the DB Server processing all the SQL Querys
268 Stats [2] - Total of Time Spent by the DB Server processing the last SQL Query
269 Stats [3] - Total of Time Spent by the DB Server transfering all the results of the SQL Querys
270 Stats [4] - Total of Time Spent by the DB Server transfering the result of the last SQL Query
271
272 Stats [5] - Total number of Rows returned by the server
273 Stats [6] - Total of Bytes Transfered by the DB Server on all SQL Querys
274 Stats [7] - Total of Bytes Transfered by the DB Server on the last SQL Query
275 Stats [8] - Number of querys made to the DBserver
276 */
277
278 short i;
279 MYDDAS_STATS_STRUCT new = NULL ;
280 MYDDAS_STATS_STRUCT first;
281 /* For the time statistics */
282
283 /* First */
284 new = myddas_stats_initialize_stat(new,time_str);
285 first = new;
286 for(i=0;i<3;i++){
287 new = myddas_stats_initialize_stat(new,time_str);
288 }
289
290 /* For number statistics*/
291 for (i=0;i<4;i++){
292 new = myddas_stats_initialize_stat(new,integer);
293 }
294
295 return first;
296 }
297
298 MYDDAS_STATS_STRUCT
myddas_stats_initialize_stat(MYDDAS_STATS_STRUCT stat,int type)299 myddas_stats_initialize_stat(MYDDAS_STATS_STRUCT stat,int type){
300
301 MYDDAS_STATS_STRUCT temp_str = stat;
302
303 if (stat == NULL){
304 MYDDAS_MALLOC(stat,struct myddas_stats_struct);
305 temp_str = stat;
306 } else {
307 for (;temp_str->nxt != NULL;temp_str = temp_str->nxt);
308 MYDDAS_MALLOC(temp_str->nxt,struct myddas_stats_struct);
309 temp_str = temp_str->nxt;
310 }
311
312 if (type == time_str){
313 MYDDAS_STATS_INITIALIZE_TIME_STRUCT(temp_str->u.time_str.time_str,time_final);
314 } else {
315 temp_str->u.integer.integer = 0;
316 }
317 temp_str->type = type;
318 temp_str->count = 0;
319 temp_str->nxt = NULL;
320 return temp_str;
321 }
322
323 MYDDAS_STATS_STRUCT
myddas_stats_get_stat(MYDDAS_STATS_STRUCT stat,int index)324 myddas_stats_get_stat(MYDDAS_STATS_STRUCT stat,int index){
325
326 MYDDAS_STATS_STRUCT temp = stat;
327
328 for (;index>1;index--){
329 temp = temp->nxt;
330 }
331 return temp;
332 }
333
334 void
myddas_stats_delete_stats_list(MYDDAS_STATS_STRUCT list)335 myddas_stats_delete_stats_list(MYDDAS_STATS_STRUCT list){
336
337 MYDDAS_STATS_STRUCT to_delete = list;
338
339 for (;to_delete!=NULL;){
340 list = list->nxt;
341
342
343 if (to_delete->type == time_str){
344 MYDDAS_FREE(to_delete->u.time_str.time_str,struct myddas_stats_time_struct);
345 }
346
347 MYDDAS_FREE(to_delete,struct myddas_stats_struct);
348
349 to_delete = list;
350 }
351
352 }
353
354
355 #endif /* MYDDAS_STATS || MYDDAS_TOP_LEVEL */
356
357