1 /*
2 * Copyright (c) 2001 Mark Fullmer and The Ohio State University
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $Id: ftprof.c,v 1.7 2003/02/13 02:38:42 maf Exp $
27 */
28
29 #include "ftconfig.h"
30 #include "ftlib.h"
31
32 #include <sys/time.h>
33 #include <stdio.h>
34
35 #if HAVE_STRINGS_H
36 #include <strings.h>
37 #endif
38 #if HAVE_STRING_H
39 #include <string.h>
40 #endif
41
42 #if HAVE_INTTYPES_H
43 # include <inttypes.h> /* C99 uint8_t uint16_t uint32_t uint64_t */
44 #elif HAVE_STDINT_H
45 # include <stdint.h> /* or here */
46 #endif /* else commit suicide. later */
47
48 /*
49 * function: ftprof_start
50 *
51 * Call before flow processing to initialize profiling.
52 *
53 * returns: < 0 error
54 * >= 0 ok
55 */
ftprof_start(struct ftprof * ftp)56 int ftprof_start(struct ftprof *ftp)
57 {
58 bzero (ftp, sizeof (struct ftprof));
59 return gettimeofday(&ftp->t0, (struct timezone*)0L);
60 }
61
62 /*
63 * function: ftprof_end
64 *
65 * Call after flow processing to finish profiling.
66 *
67 * returns: < 0 error
68 * >= 0 ok
69 */
ftprof_end(struct ftprof * ftp,uint64_t nflows)70 int ftprof_end(struct ftprof *ftp, uint64_t nflows)
71 {
72 int ret;
73
74 if ((ret = gettimeofday(&ftp->t1, (struct timezone*)0L)) == -1)
75 return -1;
76
77 if ((ret = getrusage(RUSAGE_SELF, &ftp->r0)) == -1)
78 return -1;
79
80 ftp->nflows = nflows;
81
82 return 0;
83 }
84
85 /*
86 * function: ftprof_print
87 *
88 * Dump ftprof contents to std
89 *
90 * returns: < 0 error
91 * >= 0 ok
92 */
93
ftprof_print_time(FILE * std,const char * prefix,uint32_t sec,uint32_t usec,uint64_t nflows)94 static void ftprof_print_time(FILE* std, const char * prefix, uint32_t sec, uint32_t usec, uint64_t nflows) {
95 fprintf(std, " %s: seconds=%" PRIu32 ".%-3.3" PRIu32 " flows/second=%f\n",
96 prefix, sec, usec/1000,
97 (double) nflows / ((double)sec + ((double)usec/1000000)));
98 }
99
ftprof_print(struct ftprof * ftp,char * prog,FILE * std)100 void ftprof_print(struct ftprof *ftp, char *prog, FILE *std)
101 {
102
103 char fmt_buf[256];
104 uint32_t usec, sec;
105
106 fmt_uint64(fmt_buf, ftp->nflows, FMT_JUST_LEFT);
107
108 usec = ftp->r0.ru_utime.tv_usec + ftp->r0.ru_stime.tv_usec;
109 sec = ftp->r0.ru_utime.tv_sec + ftp->r0.ru_stime.tv_sec;
110
111 if (usec > 1000000)
112 usec -= 1000000, ++sec;
113
114 fprintf(std, "%s: processed %s flows\n", prog, fmt_buf);
115 ftprof_print_time(std, "sys", sec, usec/1000, ftp->nflows);
116
117 if (ftp->t1.tv_usec < ftp->t0.tv_usec)
118 ftp->t1.tv_usec += 1000000, --ftp->t1.tv_sec;
119
120 usec = ftp->t1.tv_usec - ftp->t0.tv_usec;
121 sec = ftp->t1.tv_sec - ftp->t0.tv_sec;
122
123 ftprof_print_time(std, "sys", sec, usec/1000, ftp->nflows);
124 }
125
126