File: profiler.c

package info (click to toggle)
nws 2.11-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,700 kB
  • ctags: 2,820
  • sloc: ansic: 28,849; sh: 3,289; java: 1,205; makefile: 697; perl: 12
file content (170 lines) | stat: -rwxr-xr-x 3,153 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* $Id: profiler.c,v 1.8 2003/01/29 05:31:39 graziano Exp $ */

#include "config_nws.h"
#include <stdio.h>        /* FILE */
#include <unistd.h>       /* getpid() */
#include <sys/types.h>    /* getpid() */
#include <sys/time.h>     /* gettimeofday() */
#include <sys/resource.h> /* getrusage() */
#include "profiler.h"


#ifdef HAVE_GETRUSAGE


#ifndef timerclear
#define timerclear(tvp)     (tvp)->tv_sec = (tvp)->tv_usec = 0
#endif


static struct timeval lastUser;
static struct timeval lastSystem;
static struct timeval lastReal;
static struct timeval markLastUser;
static struct timeval markLastSystem;
static struct timeval markLastReal;
static struct rusage rusageBuffer;


int
getrusage(int who,
          struct rusage *rusage);


void
ProfWriteTime(const char *s) {
	char filename[40];
	FILE *pFile;
	struct timeval tp;
	sprintf(filename, "/tmp/complibPerf%ld", (long int)getpid());
	pFile=fopen(filename, "a");
	gettimeofday(&tp, NULL);
	fprintf(pFile, "%s:%ld.%ld\n", s, (long int)tp.tv_sec, (long int)tp.tv_usec);
	fclose(pFile);
}


int
ProfStart() {
	getrusage(RUSAGE_SELF,  &rusageBuffer);
	lastUser = rusageBuffer.ru_utime;
	lastSystem = rusageBuffer.ru_stime;
	gettimeofday(&lastReal, NULL);
	timerclear(&markLastSystem);
	timerclear(&markLastUser);
	timerclear(&markLastReal);
	/* writeTime("start"); */
	return 1;
}


void
ProfStop() {
	getrusage(RUSAGE_SELF,  &rusageBuffer);
	markLastUser.tv_sec = rusageBuffer.ru_utime.tv_sec - lastUser.tv_sec;
	markLastUser.tv_usec = rusageBuffer.ru_utime.tv_usec - lastUser.tv_usec;
	if(markLastUser.tv_usec < 0) {
		markLastUser.tv_usec += 1000000; 
		markLastUser.tv_sec-=1;
	}
	markLastSystem.tv_sec = rusageBuffer.ru_stime.tv_sec - lastSystem.tv_sec;
	markLastSystem.tv_usec = rusageBuffer.ru_stime.tv_usec - lastSystem.tv_usec;
	if(markLastSystem.tv_usec < 0) {
		markLastSystem.tv_usec += 1000000; 
		markLastSystem.tv_sec-=1;
	}
	gettimeofday(&markLastReal, NULL);
	markLastReal.tv_sec -= lastReal.tv_sec;
	markLastReal.tv_usec -= lastReal.tv_usec;
	if(markLastReal.tv_usec < 0) {
		markLastReal.tv_usec += 1000000; 
		markLastReal.tv_sec-=1;
	}
	/* writeTime("stop"); */
}


unsigned long
ProfMsec() {
	return (markLastUser.tv_sec*1000 + markLastSystem.tv_sec*1000
		 + markLastUser.tv_usec/1000 + markLastSystem.tv_usec/1000);
}


unsigned long
ProfUsec() {
	return (markLastUser.tv_sec*1000000 + markLastUser.tv_usec +
		markLastSystem.tv_sec*1000000 + markLastSystem.tv_usec);
}


unsigned long
ProfMsecR() {
	return (markLastReal.tv_sec*1000 + markLastReal.tv_usec/1000);
}


unsigned long
ProfUsecR() {
	return (markLastReal.tv_sec*1000000 + markLastReal.tv_usec);
}


/* take advantage of non-destructive stop, but hide the decision here */
unsigned long
ProfElapsedMsecR() {
	ProfStop();
	return ProfMsecR();
}


#else


int
ProfStart(void) {
  return 0;
}


void
ProfStop(void) {
}


unsigned long
ProfMsec(void) {
  return 0;
}

 
unsigned long
ProfUsec(void) {
  return 0;
}


unsigned long
ProfMsecR(void) {
  return 0;
}


unsigned long
ProfUsecR(void) {
  return 0;
}


unsigned long
ProfElapsedMsecR(void) {
  return 0;
}


void
ProfWriteTime(const char *s) {
}


#endif