1235063Snetchild#!/usr/sbin/dtrace -qs
2235063Snetchild
3235063Snetchild/*-
4235063Snetchild * Copyright (c) 2008-2012 Alexander Leidinger <netchild@FreeBSD.org>
5235063Snetchild * All rights reserved.
6235063Snetchild *
7235063Snetchild * Redistribution and use in source and binary forms, with or without
8235063Snetchild * modification, are permitted provided that the following conditions
9235063Snetchild * are met:
10235063Snetchild * 1. Redistributions of source code must retain the above copyright
11235063Snetchild *    notice, this list of conditions and the following disclaimer
12235063Snetchild *    in this position and unchanged.
13235063Snetchild * 2. Redistributions in binary form must reproduce the above copyright
14235063Snetchild *    notice, this list of conditions and the following disclaimer in the
15235063Snetchild *    documentation and/or other materials provided with the distribution.
16235063Snetchild *
17235063Snetchild * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18235063Snetchild * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19235063Snetchild * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20235063Snetchild * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21235063Snetchild * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22235063Snetchild * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23235063Snetchild * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24235063Snetchild * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25235063Snetchild * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26235063Snetchild * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27235063Snetchild *
28235063Snetchild * $FreeBSD: releng/10.3/sys/compat/linux/stats_timing.d 293493 2016-01-09 15:16:13Z dchagin $
29235063Snetchild */
30235063Snetchild
31235063Snetchild/**
32235063Snetchild * Some statistics (all per provider):
33235063Snetchild *  - number of calls to a function per executable binary (not per PID!)
34235063Snetchild *    - allows to see where an optimization would be beneficial for a given
35235063Snetchild *      application
36235063Snetchild *  - graph of CPU time spend in functions per executable binary
37235063Snetchild *    - together with the number of calls to this function this allows
38235063Snetchild *      to determine if a kernel optimization would be beneficial / is
39235063Snetchild *      possible for a given application
40235063Snetchild *  - graph of longest running (CPU-time!) function in total
41235063Snetchild *    - may help finding problem cases in the kernel code
42235063Snetchild * - graph of longest held (CPU-time!) locks
43235063Snetchild */
44235063Snetchild
45235063Snetchild#pragma D option dynvarsize=32m
46235063Snetchild
47235063Snetchildlinuxulator*:::entry
48235063Snetchild{
49235063Snetchild	self->time[probefunc] = vtimestamp;
50235063Snetchild	@calls[probeprov, execname, probefunc] = count();
51235063Snetchild}
52235063Snetchild
53235063Snetchildlinuxulator*:::return
54235063Snetchild/self->time[probefunc] != 0/
55235063Snetchild{
56235063Snetchild	this->timediff = self->time[probefunc] - vtimestamp;
57235063Snetchild
58235063Snetchild	@stats[probeprov, execname, probefunc] = quantize(this->timediff);
59235063Snetchild	@longest[probeprov, probefunc] = max(this->timediff);
60235063Snetchild
61235063Snetchild	self->time[probefunc] = 0;
62235063Snetchild}
63235063Snetchild
64235063Snetchildlinuxulator*:::locked
65235063Snetchild{
66235063Snetchild	self->lock[arg0] = vtimestamp;
67235063Snetchild}
68235063Snetchild
69235063Snetchildlinuxulator*:::unlock
70235063Snetchild/self->lock[arg0] != 0/
71235063Snetchild{
72235063Snetchild	this->timediff = self->lock[arg0] - vtimestamp;
73235063Snetchild
74235063Snetchild	@lockstats[probefunc] = quantize(this->timediff);
75235063Snetchild	@longlock[probefunc] = max(this->timediff);
76235063Snetchild
77235063Snetchild	self->lock[arg0] = 0;
78235063Snetchild}
79235063Snetchild
80235063SnetchildEND
81235063Snetchild{
82235063Snetchild	printf("Number of calls per provider/application/kernel function:");
83235063Snetchild	printa(@calls);
84235063Snetchild	printf("CPU-timing statistics per provider/application/kernel function (in ns):");
85235063Snetchild	printa(@stats);
86235063Snetchild	printf("Longest running (CPU-time!) functions per provider (in ns):");
87235063Snetchild	printa(@longest);
88235063Snetchild	printf("Lock CPU-timing statistics:");
89235063Snetchild	printa(@lockstats);
90235063Snetchild	printf("Longest running (CPU-time!) locks:");
91235063Snetchild	printa(@longlock);
92235063Snetchild}
93235063Snetchild
94