1 /*
2  *  Copyright (c) 2014, Peter Haag
3  *  Copyright (c) 2009, Peter Haag
4  *  Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions are met:
9  *
10  *   * Redistributions of source code must retain the above copyright notice,
11  *     this list of conditions and the following disclaimer.
12  *   * Redistributions in binary form must reproduce the above copyright notice,
13  *     this list of conditions and the following disclaimer in the documentation
14  *     and/or other materials provided with the distribution.
15  *   * Neither the name of the author nor the names of its contributors may be
16  *     used to endorse or promote products derived from this software without
17  *     specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  *
31  *  $Author: haag $
32  *
33  *  $Id: nfprof.c 39 2009-11-25 08:11:15Z haag $
34  *
35  *  $LastChangedRevision: 39 $
36  *
37  */
38 
39 #include "config.h"
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <sys/types.h>
44 #include <strings.h>
45 
46 #ifdef HAVE_STDINT_H
47 #include <stdint.h>
48 #endif
49 
50 #include <sys/time.h>
51 #include <sys/resource.h>
52 #include "nfprof.h"
53 
54 /*
55  * Initialize profiling.
56  *
57  */
nfprof_start(nfprof_t * profile_data)58 int nfprof_start(nfprof_t *profile_data) {
59 
60 	bzero (profile_data, sizeof(nfprof_t));
61 	return gettimeofday(&profile_data->tstart, (struct timezone*)NULL) == 0 ? 1 : 0;
62 
63 } // End of nfprof_start
64 
65 /*
66  * Finish profiling.
67  *
68  */
nfprof_end(nfprof_t * profile_data,uint64_t numflows)69 int nfprof_end(nfprof_t *profile_data, uint64_t numflows) {
70 int ret;
71 
72 	if ((ret = gettimeofday(&profile_data->tend, (struct timezone*)NULL)) == -1)
73 		return 1;
74 
75 	if ((ret = getrusage(RUSAGE_SELF, &profile_data->used)) == -1)
76 		return 1;
77 
78 	profile_data->numflows = numflows;
79 
80 	return 0;
81 
82 } // End of nfprof_end
83 
84 /*
85  * Dump nfprof contents to std
86  *
87  */
nfprof_print(nfprof_t * profile_data,FILE * std)88 void  nfprof_print(nfprof_t *profile_data, FILE *std) {
89 u_long usec, sec;
90 double fps, allsecs;
91 
92 	usec = profile_data->used.ru_utime.tv_usec + profile_data->used.ru_stime.tv_usec;
93 	sec = profile_data->used.ru_utime.tv_sec + profile_data->used.ru_stime.tv_sec;
94 
95 	if (usec > 1000000)
96 		usec -= 1000000, ++sec;
97 
98 
99 	allsecs = (double)sec + ((double)usec/1000000);
100 	if ( allsecs == 0.0 )
101 		fps = 0;
102 	else
103 		fps = (double)profile_data->numflows / ((double)sec + ((double)usec/1000000));
104 
105 	fprintf(std, "Sys: %lu.%-3.3lus flows/second: %-10.1f ", sec, usec/1000, fps);
106 
107 	if (profile_data->tend.tv_usec < profile_data->tstart.tv_usec)
108 		profile_data->tend.tv_usec += 1000000, --profile_data->tend.tv_sec;
109 
110 	usec = profile_data->tend.tv_usec - profile_data->tstart.tv_usec;
111 	sec = profile_data->tend.tv_sec - profile_data->tstart.tv_sec;
112 
113 	if ( usec == 0 && sec == 0 )
114 		// acctually should never happen, but catch it anyway
115 		fps = 0;
116 	else
117 		fps = (double)profile_data->numflows / ((double)sec + ((double)usec/1000000));
118 
119 	fprintf(std, "Wall: %lu.%-3.3lus flows/second: %-10.1f\n", sec, usec/1000, fps);
120 /*
121 	fprintf(std, "\n");
122 	fprintf(std, "integral max resident set size: %u\n", profile_data->used.ru_maxrss);
123 	fprintf(std, "integral shared text memory size: %u\n", profile_data->used.ru_ixrss);
124 	fprintf(std, "integral unshared data size: %u\n", profile_data->used.ru_idrss);
125 	fprintf(std, "integral unshared stack size: %u\n", profile_data->used.ru_isrss);
126 	fprintf(std, "page reclaims: %u\n", profile_data->used.ru_minflt);
127 	fprintf(std, "page faults: %u\n", profile_data->used.ru_majflt);
128 	fprintf(std, "swaps: %u\n", profile_data->used.ru_nswap);
129 	fprintf(std, "block input operations: %u\n", profile_data->used.ru_inblock);
130 	fprintf(std, "block output operations: %u\n", profile_data->used.ru_oublock);
131 */
132 
133 } // End of nfprof_print
134 
135