Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*-
 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
 *
 * Copyright (c) 2008 Mellanox Technologies Ltd.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/proc_fs.h>
#include "sdp.h"

#ifdef CONFIG_PROC_FS

#define PROC_SDP_STATS "sdpstats"
#define PROC_SDP_PERF "sdpprf"

/* just like TCP fs */
struct sdp_seq_afinfo {
	struct module           *owner;
	char                    *name;
	sa_family_t             family;
	int                     (*seq_show) (struct seq_file *m, void *v);
	struct file_operations  *seq_fops;
};

struct sdp_iter_state {
	sa_family_t             family;
	int                     num;
	struct seq_operations   seq_ops;
};

static void *sdp_get_idx(struct seq_file *seq, loff_t pos)
{
	int i = 0;
	struct sdp_sock *ssk;

	if (!list_empty(&sock_list))
		list_for_each_entry(ssk, &sock_list, sock_list) {
			if (i == pos)
				return ssk;
			i++;
		}

	return NULL;
}

static void *sdp_seq_start(struct seq_file *seq, loff_t *pos)
{
	void *start = NULL;
	struct sdp_iter_state *st = seq->private;

	st->num = 0;

	if (!*pos)
		return SEQ_START_TOKEN;

	spin_lock_irq(&sock_list_lock);
	start = sdp_get_idx(seq, *pos - 1);
	if (start)
		sock_hold((struct socket *)start, SOCK_REF_SEQ);
	spin_unlock_irq(&sock_list_lock);

	return start;
}

static void *sdp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	struct sdp_iter_state *st = seq->private;
	void *next = NULL;

	spin_lock_irq(&sock_list_lock);
	if (v == SEQ_START_TOKEN)
		next = sdp_get_idx(seq, 0);
	else
		next = sdp_get_idx(seq, *pos);
	if (next)
		sock_hold((struct socket *)next, SOCK_REF_SEQ);
	spin_unlock_irq(&sock_list_lock);

	*pos += 1;
	st->num++;

	return next;
}

static void sdp_seq_stop(struct seq_file *seq, void *v)
{
}

#define TMPSZ 150

static int sdp_seq_show(struct seq_file *seq, void *v)
{
	struct sdp_iter_state *st;
	struct socket *sk = v;
	char tmpbuf[TMPSZ + 1];
	unsigned int dest;
	unsigned int src;
	int uid;
	unsigned long inode;
	__u16 destp;
	__u16 srcp;
	__u32 rx_queue, tx_queue;

	if (v == SEQ_START_TOKEN) {
		seq_printf(seq, "%-*s\n", TMPSZ - 1,
				"  sl  local_address rem_address        "
				"uid inode   rx_queue tx_queue state");
		goto out;
	}

	st = seq->private;

	dest = inet_sk(sk)->daddr;
	src = inet_sk(sk)->rcv_saddr;
	destp = ntohs(inet_sk(sk)->dport);
	srcp = ntohs(inet_sk(sk)->sport);
	uid = sock_i_uid(sk);
	inode = sock_i_ino(sk);
	rx_queue = rcv_nxt(sdp_sk(sk)) - sdp_sk(sk)->copied_seq;
	tx_queue = sdp_sk(sk)->write_seq - sdp_sk(sk)->tx_ring.una_seq;

	sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X %5d %lu	%08X:%08X %X",
		st->num, src, srcp, dest, destp, uid, inode,
		rx_queue, tx_queue, sk->sk_state);

	seq_printf(seq, "%-*s\n", TMPSZ - 1, tmpbuf);

	sock_put(sk, SOCK_REF_SEQ);
out:
	return 0;
}

static int sdp_seq_open(struct inode *inode, struct file *file)
{
	struct sdp_seq_afinfo *afinfo = PDE(inode)->data;
	struct seq_file *seq;
	struct sdp_iter_state *s;
	int rc;

	if (unlikely(afinfo == NULL))
		return -EINVAL;

/* Workaround bogus warning by memtrack */
#define _kzalloc(size,flags) kzalloc(size,flags)
#undef kzalloc
	s = kzalloc(sizeof(*s), GFP_KERNEL);
#define kzalloc(s,f) _kzalloc(s,f)	
	if (!s)
		return -ENOMEM;
	s->family               = afinfo->family;
	s->seq_ops.start        = sdp_seq_start;
	s->seq_ops.next         = sdp_seq_next;
	s->seq_ops.show         = afinfo->seq_show;
	s->seq_ops.stop         = sdp_seq_stop;

	rc = seq_open(file, &s->seq_ops);
	if (rc)
		goto out_kfree;
	seq          = file->private_data;
	seq->private = s;
out:
	return rc;
out_kfree:
	kfree(s);
	goto out;
}


static struct file_operations sdp_seq_fops;
static struct sdp_seq_afinfo sdp_seq_afinfo = {
	.owner          = THIS_MODULE,
	.name           = "sdp",
	.family         = AF_INET_SDP,
	.seq_show       = sdp_seq_show,
	.seq_fops       = &sdp_seq_fops,
};

#ifdef SDPSTATS_ON
DEFINE_PER_CPU(struct sdpstats, sdpstats);

static void sdpstats_seq_hist(struct seq_file *seq, char *str, u32 *h, int n,
		int is_log)
{
	int i;
	u32 max = 0;

	seq_printf(seq, "%s:\n", str);

	for (i = 0; i < n; i++) {
		if (h[i] > max)
			max = h[i];
	}

	if (max == 0) {
		seq_printf(seq, " - all values are 0\n");
		return;
	}

	for (i = 0; i < n; i++) {
		char s[51];
		int j = 50 * h[i] / max;
		int val = is_log ? (i == n-1 ? 0 : 1<<i) : i;
		memset(s, '*', j);
		s[j] = '\0';

		seq_printf(seq, "%10d | %-50s - %d\n", val, s, h[i]);
	}
}

#define SDPSTATS_COUNTER_GET(var) ({ \
	u32 __val = 0;						\
	unsigned int __i;                                       \
	for_each_possible_cpu(__i)                              \
		__val += per_cpu(sdpstats, __i).var;		\
	__val;							\
})	

#define SDPSTATS_HIST_GET(hist, hist_len, sum) ({ \
	unsigned int __i;                                       \
	for_each_possible_cpu(__i) {                            \
		unsigned int __j;				\
		u32 *h = per_cpu(sdpstats, __i).hist;		\
		for (__j = 0; __j < hist_len; __j++) { 		\
			sum[__j] += h[__j];			\
		} \
	} 							\
})

#define __sdpstats_seq_hist(seq, msg, hist, is_log) ({		\
	u32 tmp_hist[SDPSTATS_MAX_HIST_SIZE];			\
	int hist_len = ARRAY_SIZE(__get_cpu_var(sdpstats).hist);\
	memset(tmp_hist, 0, sizeof(tmp_hist));			\
	SDPSTATS_HIST_GET(hist, hist_len, tmp_hist);	\
	sdpstats_seq_hist(seq, msg, tmp_hist, hist_len, is_log);\
})

static int sdpstats_seq_show(struct seq_file *seq, void *v)
{
	int i;

	seq_printf(seq, "SDP statistics:\n");

	__sdpstats_seq_hist(seq, "sendmsg_seglen", sendmsg_seglen, 1);
	__sdpstats_seq_hist(seq, "send_size", send_size, 1);
	__sdpstats_seq_hist(seq, "credits_before_update",
		credits_before_update, 0);

	seq_printf(seq, "sdp_sendmsg() calls\t\t: %d\n",
		SDPSTATS_COUNTER_GET(sendmsg));
	seq_printf(seq, "bcopy segments     \t\t: %d\n",
		SDPSTATS_COUNTER_GET(sendmsg_bcopy_segment));
	seq_printf(seq, "bzcopy segments    \t\t: %d\n",
		SDPSTATS_COUNTER_GET(sendmsg_bzcopy_segment));
	seq_printf(seq, "zcopy segments    \t\t: %d\n",
		SDPSTATS_COUNTER_GET(sendmsg_zcopy_segment));
	seq_printf(seq, "post_send_credits  \t\t: %d\n",
		SDPSTATS_COUNTER_GET(post_send_credits));
	seq_printf(seq, "memcpy_count       \t\t: %u\n",
		SDPSTATS_COUNTER_GET(memcpy_count));

        for (i = 0; i < ARRAY_SIZE(__get_cpu_var(sdpstats).post_send); i++) {
                if (mid2str(i)) {
                        seq_printf(seq, "post_send %-20s\t: %d\n",
                                        mid2str(i),
					SDPSTATS_COUNTER_GET(post_send[i]));
                }
        }

	seq_printf(seq, "\n");
	seq_printf(seq, "post_recv         \t\t: %d\n",
		SDPSTATS_COUNTER_GET(post_recv));
	seq_printf(seq, "BZCopy poll miss  \t\t: %d\n",
		SDPSTATS_COUNTER_GET(bzcopy_poll_miss));
	seq_printf(seq, "send_wait_for_mem \t\t: %d\n",
		SDPSTATS_COUNTER_GET(send_wait_for_mem));
	seq_printf(seq, "send_miss_no_credits\t\t: %d\n",
		SDPSTATS_COUNTER_GET(send_miss_no_credits));

	seq_printf(seq, "rx_poll_miss      \t\t: %d\n", SDPSTATS_COUNTER_GET(rx_poll_miss));
	seq_printf(seq, "tx_poll_miss      \t\t: %d\n", SDPSTATS_COUNTER_GET(tx_poll_miss));
	seq_printf(seq, "tx_poll_busy      \t\t: %d\n", SDPSTATS_COUNTER_GET(tx_poll_busy));
	seq_printf(seq, "tx_poll_hit       \t\t: %d\n", SDPSTATS_COUNTER_GET(tx_poll_hit));

	seq_printf(seq, "CQ stats:\n");
	seq_printf(seq, "- RX interrupts\t\t: %d\n", SDPSTATS_COUNTER_GET(rx_int_count));
	seq_printf(seq, "- TX interrupts\t\t: %d\n", SDPSTATS_COUNTER_GET(tx_int_count));

	seq_printf(seq, "ZCopy stats:\n");
	seq_printf(seq, "- TX timeout\t\t: %d\n", SDPSTATS_COUNTER_GET(zcopy_tx_timeout));
	seq_printf(seq, "- TX cross send\t\t: %d\n", SDPSTATS_COUNTER_GET(zcopy_cross_send));
	seq_printf(seq, "- TX aborted by peer\t: %d\n", SDPSTATS_COUNTER_GET(zcopy_tx_aborted));
	seq_printf(seq, "- TX error\t\t: %d\n", SDPSTATS_COUNTER_GET(zcopy_tx_error));
	return 0;
}

static ssize_t sdpstats_write(struct file *file, const char __user *buf,
			    size_t count, loff_t *offs)
{
	int i;

	for_each_possible_cpu(i)
		memset(&per_cpu(sdpstats, i), 0, sizeof(struct sdpstats));
	printk(KERN_WARNING "Cleared sdp statistics\n");

	return count;
}

static int sdpstats_seq_open(struct inode *inode, struct file *file)
{
	return single_open(file, sdpstats_seq_show, NULL);
}

static struct file_operations sdpstats_fops = {
	.owner		= THIS_MODULE,
	.open		= sdpstats_seq_open,
	.read		= seq_read,
	.write		= sdpstats_write,
	.llseek		= seq_lseek,
	.release	= single_release,
};

#endif

#ifdef SDP_PROFILING
struct sdpprf_log sdpprf_log[SDPPRF_LOG_SIZE];
int sdpprf_log_count;

static unsigned long long start_t;

static int sdpprf_show(struct seq_file *m, void *v)
{
	struct sdpprf_log *l = v;
	unsigned long nsec_rem, t;

	if (!sdpprf_log_count) {
		seq_printf(m, "No performance logs\n");
		goto out;
	}

	t = l->time - start_t;
	nsec_rem = do_div(t, 1000000000);

	seq_printf(m, "%-6d: [%5lu.%06lu] %-50s - [%d{%d} %d:%d] "
			"mb: %p %s:%d\n",
			l->idx, (unsigned long)t, nsec_rem/1000,
			l->msg, l->pid, l->cpu, l->sk_num, l->sk_dport,
			l->mb, l->func, l->line);
out:
	return 0;
}

static void *sdpprf_start(struct seq_file *p, loff_t *pos)
{
	int idx = *pos;

	if (!*pos) {
		if (!sdpprf_log_count)
			return SEQ_START_TOKEN;
	}

	if (*pos >= MIN(sdpprf_log_count, SDPPRF_LOG_SIZE - 1))
		return NULL;

	if (sdpprf_log_count >= SDPPRF_LOG_SIZE - 1) {
		int off = sdpprf_log_count & (SDPPRF_LOG_SIZE - 1);
		idx = (idx + off) & (SDPPRF_LOG_SIZE - 1);

	}

	if (!start_t)
		start_t = sdpprf_log[idx].time;
	return &sdpprf_log[idx];
}

static void *sdpprf_next(struct seq_file *p, void *v, loff_t *pos)
{
	struct sdpprf_log *l = v;

	if (++*pos >= MIN(sdpprf_log_count, SDPPRF_LOG_SIZE - 1))
		return NULL;

	++l;
	if (l - &sdpprf_log[0] >= SDPPRF_LOG_SIZE - 1)
		return &sdpprf_log[0];

	return l;
}

static void sdpprf_stop(struct seq_file *p, void *v)
{
}

static struct seq_operations sdpprf_ops = {
	.start = sdpprf_start,
	.stop = sdpprf_stop,
	.next = sdpprf_next,
	.show = sdpprf_show,
};

static int sdpprf_open(struct inode *inode, struct file *file)
{
	int res;

	res = seq_open(file, &sdpprf_ops);

	return res;
}

static ssize_t sdpprf_write(struct file *file, const char __user *buf,
			    size_t count, loff_t *offs)
{
	sdpprf_log_count = 0;
	printk(KERN_INFO "Cleared sdpprf statistics\n");

	return count;
}

static struct file_operations sdpprf_fops = {
	.open           = sdpprf_open,
	.read           = seq_read,
	.llseek         = seq_lseek,
	.release        = seq_release,
	.write		= sdpprf_write,
};
#endif /* SDP_PROFILING */

int __init sdp_proc_init(void)
{
	struct proc_dir_entry *p = NULL;
#ifdef SDPSTATS_ON
	struct proc_dir_entry *stats = NULL;
#endif
#ifdef SDP_PROFILING
	struct proc_dir_entry *prof = NULL;
#endif

	sdp_seq_afinfo.seq_fops->owner         = sdp_seq_afinfo.owner;
	sdp_seq_afinfo.seq_fops->open          = sdp_seq_open;
	sdp_seq_afinfo.seq_fops->read          = seq_read;
	sdp_seq_afinfo.seq_fops->llseek        = seq_lseek;
	sdp_seq_afinfo.seq_fops->release       = seq_release_private;

	p = proc_net_fops_create(&init_net, sdp_seq_afinfo.name, S_IRUGO,
				 sdp_seq_afinfo.seq_fops);
	if (p)
		p->data = &sdp_seq_afinfo;
	else
		goto no_mem;

#ifdef SDPSTATS_ON

	stats = proc_net_fops_create(&init_net, PROC_SDP_STATS,
			S_IRUGO | S_IWUGO, &sdpstats_fops);
	if (!stats)
		goto no_mem_stats;

#endif

#ifdef SDP_PROFILING
	prof = proc_net_fops_create(&init_net, PROC_SDP_PERF,
			S_IRUGO | S_IWUGO, &sdpprf_fops);
	if (!prof)
		goto no_mem_prof;
#endif

	return 0;

#ifdef SDP_PROFILING
no_mem_prof:
#endif

#ifdef SDPSTATS_ON
	proc_net_remove(&init_net, PROC_SDP_STATS);

no_mem_stats:
#endif
	proc_net_remove(&init_net, sdp_seq_afinfo.name);

no_mem:	
	return -ENOMEM;
}

void sdp_proc_unregister(void)
{
	proc_net_remove(&init_net, sdp_seq_afinfo.name);
	memset(sdp_seq_afinfo.seq_fops, 0, sizeof(*sdp_seq_afinfo.seq_fops));

#ifdef SDPSTATS_ON
	proc_net_remove(&init_net, PROC_SDP_STATS);
#endif
#ifdef SDP_PROFILING
	proc_net_remove(&init_net, PROC_SDP_PERF);
#endif
}

#else /* CONFIG_PROC_FS */

int __init sdp_proc_init(void)
{
	return 0;
}

void sdp_proc_unregister(void)
{

}
#endif /* CONFIG_PROC_FS */