summaryrefslogtreecommitdiffstats
path: root/include/net/profile.h
blob: 5393f0d04154e2fdf33a9bbcaef14535311f964b (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <linux/config.h> /* for CONFIG_NET_PROFILE */
#ifndef _NET_PROFILE_H_
#define _NET_PROFILE_H_ 1

#ifdef CONFIG_NET_PROFILE

#include <linux/types.h>
#include <linux/time.h>
#include <linux/kernel.h>
#include <asm/system.h>

#ifdef CONFIG_X86_TSC
#include <asm/msr.h>
#endif

struct net_profile_slot
{
	char   id[16];
	struct net_profile_slot *next;
	struct timeval entered;
	struct timeval accumulator;
	struct timeval irq;
	int    	hits;
	int    	active;
	int	underflow;
};

extern atomic_t net_profile_active;
extern struct timeval net_profile_adjust;
extern void net_profile_irq_adjust(struct timeval *entered, struct timeval* leaved);

#ifdef CONFIG_X86_TSC

static inline void  net_profile_stamp(struct timeval *pstamp)
{
	rdtsc(pstamp->tv_usec, pstamp->tv_sec);
}

static inline void  net_profile_accumulate(struct timeval *entered,
					       struct timeval *leaved,
					       struct timeval *acc)
{
	__asm__ __volatile__ ("subl %2,%0\n\t" 
			      "sbbl %3,%1\n\t" 
			      "addl %4,%0\n\t" 
			      "adcl %5,%1\n\t" 
			      "subl " SYMBOL_NAME_STR(net_profile_adjust) "+4,%0\n\t" 
			      "sbbl $0,%1\n\t" 
			      : "=r" (acc->tv_usec), "=r" (acc->tv_sec)
			      : "g" (entered->tv_usec), "g" (entered->tv_sec),
			      "g" (leaved->tv_usec), "g" (leaved->tv_sec),
			      "0" (acc->tv_usec), "1" (acc->tv_sec));
}

static inline void  net_profile_sub(struct timeval *sub,
					struct timeval *acc)
{
	__asm__ __volatile__ ("subl %2,%0\n\t" 
			      "sbbl %3,%1\n\t" 
			      : "=r" (acc->tv_usec), "=r" (acc->tv_sec)
			      : "g" (sub->tv_usec), "g" (sub->tv_sec),
			      "0" (acc->tv_usec), "1" (acc->tv_sec));
}

static inline void  net_profile_add(struct timeval *add,
					struct timeval *acc)
{
	__asm__ __volatile__ ("addl %2,%0\n\t" 
			      "adcl %3,%1\n\t" 
			      : "=r" (acc->tv_usec), "=r" (acc->tv_sec)
			      : "g" (add->tv_usec), "g" (add->tv_sec),
			      "0" (acc->tv_usec), "1" (acc->tv_sec));
}


#elif defined (__alpha__)

extern __u32 alpha_lo;
extern long alpha_hi;

/* On alpha cycle counter has only 32 bits :-( :-( */

static inline void  net_profile_stamp(struct timeval *pstamp)
{
	__u32 result;
	__asm__ __volatile__ ("rpcc %0" : "r="(result));
	if (result <= alpha_lo)
		alpha_hi++;
	alpha_lo = result;
	pstamp->tv_sec = alpha_hi;
	pstamp->tv_usec = alpha_lo;
}

static inline void  net_profile_accumulate(struct timeval *entered,
					       struct timeval *leaved,
					       struct timeval *acc)
{
	time_t usecs = acc->tv_usec + leaved->tv_usec - entered->tv_usec
		- net_profile_adjust.tv_usec;
	time_t secs = acc->tv_sec + leaved->tv_sec - entered->tv_sec;

	if (usecs >= 0x100000000L) {
		usecs -= 0x100000000L;
		secs++;
	} else if (usecs < -0x100000000L) {
		usecs += 0x200000000L;
		secs -= 2;
	} else if (usecs < 0) {
		usecs += 0x100000000L;
		secs--;
	}
	acc->tv_sec = secs;
	acc->tv_usec = usecs;
}

static inline void  net_profile_sub(struct timeval *entered,
					struct timeval *leaved)
{
	time_t usecs = leaved->tv_usec - entered->tv_usec;
	time_t secs = leaved->tv_sec - entered->tv_sec;

	if (usecs < 0) {
		usecs += 0x100000000L;
		secs--;
	}
	leaved->tv_sec = secs;
	leaved->tv_usec = usecs;
}

static inline void  net_profile_add(struct timeval *entered, struct timeval *leaved)
{
	time_t usecs = leaved->tv_usec + entered->tv_usec;
	time_t secs = leaved->tv_sec + entered->tv_sec;

	if (usecs >= 0x100000000L) {
		usecs -= 0x100000000L;
		secs++;
	}
	leaved->tv_sec = secs;
	leaved->tv_usec = usecs;
}


#else

static inline void  net_profile_stamp(struct timeval *pstamp)
{
	/* Not "fast" counterpart! On architectures without
	   cpu clock "fast" routine is absolutely useless in this
	   situation. do_gettimeofday still says something on slow-slow-slow
	   boxes, though it eats more cpu time than the subject of
	   investigation :-) :-)
	 */
	do_gettimeofday(pstamp);
}

static inline void  net_profile_accumulate(struct timeval *entered,
					       struct timeval *leaved,
					       struct timeval *acc)
{
	time_t usecs = acc->tv_usec + leaved->tv_usec - entered->tv_usec
		- net_profile_adjust.tv_usec;
	time_t secs = acc->tv_sec + leaved->tv_sec - entered->tv_sec;

	if (usecs >= 1000000) {
		usecs -= 1000000;
		secs++;
	} else if (usecs < -1000000) {
		usecs += 2000000;
		secs -= 2;
	} else if (usecs < 0) {
		usecs += 1000000;
		secs--;
	}
	acc->tv_sec = secs;
	acc->tv_usec = usecs;
}

static inline void  net_profile_sub(struct timeval *entered,
					struct timeval *leaved)
{
	time_t usecs = leaved->tv_usec - entered->tv_usec;
	time_t secs = leaved->tv_sec - entered->tv_sec;

	if (usecs < 0) {
		usecs += 1000000;
		secs--;
	}
	leaved->tv_sec = secs;
	leaved->tv_usec = usecs;
}

static inline void  net_profile_add(struct timeval *entered, struct timeval *leaved)
{
	time_t usecs = leaved->tv_usec + entered->tv_usec;
	time_t secs = leaved->tv_sec + entered->tv_sec;

	if (usecs >= 1000000) {
		usecs -= 1000000;
		secs++;
	}
	leaved->tv_sec = secs;
	leaved->tv_usec = usecs;
}



#endif

static inline void net_profile_enter(struct net_profile_slot *s)
{
	unsigned long flags;

	save_flags(flags);
	cli();
	if (s->active++ == 0) {
		net_profile_stamp(&s->entered);
		atomic_inc(&net_profile_active);
	}
	restore_flags(flags);
}

static inline void net_profile_leave_irq(struct net_profile_slot *s)
{
	unsigned long flags;

	save_flags(flags);
	cli();
	if (--s->active <= 0) {
		if (s->active == 0) {
			struct timeval curr_pstamp;
			net_profile_stamp(&curr_pstamp);
			net_profile_accumulate(&s->entered, &curr_pstamp, &s->accumulator);
			if (!atomic_dec_and_test(&net_profile_active))
				net_profile_irq_adjust(&s->entered, &curr_pstamp);
		} else {
			s->underflow++;
		}
	}
	s->hits++;
	restore_flags(flags);
}

static inline void net_profile_leave(struct net_profile_slot *s)
{
	unsigned long flags;
	save_flags(flags);
	cli();
	if (--s->active <= 0) {
		if (s->active == 0) {
			struct timeval curr_pstamp;
			net_profile_stamp(&curr_pstamp);
			net_profile_accumulate(&s->entered, &curr_pstamp, &s->accumulator);
			atomic_dec(&net_profile_active);
		} else {
			s->underflow++;
		}
	}
	s->hits++;
	restore_flags(flags);
}


#define NET_PROFILE_ENTER(slot) net_profile_enter(&net_prof_##slot)
#define NET_PROFILE_LEAVE(slot) net_profile_leave(&net_prof_##slot)
#define NET_PROFILE_LEAVE_IRQ(slot) net_profile_leave_irq(&net_prof_##slot)

#define NET_PROFILE_SKB_CLEAR(skb) ({ \
 skb->pstamp.tv_usec = 0; \
})

#define NET_PROFILE_SKB_INIT(skb) ({ \
 net_profile_stamp(&skb->pstamp); \
})

#define NET_PROFILE_SKB_PASSED(skb, slot) ({ \
 if (skb->pstamp.tv_usec) { \
   struct timeval cur_pstamp = skb->pstamp; \
   net_profile_stamp(&skb->pstamp); \
   net_profile_accumulate(&cur_pstamp, &skb->pstamp, &net_prof_##slot.accumulator); \
   net_prof_##slot.hits++; \
 }})

#define NET_PROFILE_DECL(slot) \
  extern struct net_profile_slot net_prof_##slot;

#define NET_PROFILE_DEFINE(slot) \
  struct net_profile_slot net_prof_##slot = { #slot, };

#define NET_PROFILE_REGISTER(slot) net_profile_register(&net_prof_##slot)
#define NET_PROFILE_UNREGISTER(slot) net_profile_unregister(&net_prof_##slot)

extern int net_profile_init(void);
extern int net_profile_register(struct net_profile_slot *);
extern int net_profile_unregister(struct net_profile_slot *);

#else

#define NET_PROFILE_ENTER(slot) do { /* nothing */ } while(0)
#define NET_PROFILE_LEAVE(slot) do { /* nothing */ } while(0)
#define NET_PROFILE_LEAVE_IRQ(slot) do { /* nothing */ } while(0)
#define NET_PROFILE_SKB_CLEAR(skb) do { /* nothing */ } while(0)
#define NET_PROFILE_SKB_INIT(skb) do { /* nothing */ } while(0)
#define NET_PROFILE_SKB_PASSED(skb, slot) do { /* nothing */ } while(0)
#define NET_PROFILE_DECL(slot)
#define NET_PROFILE_DEFINE(slot)
#define NET_PROFILE_REGISTER(slot) do { /* nothing */ } while(0)
#define NET_PROFILE_UNREGISTER(slot) do { /* nothing */ } while(0)

#endif

#endif