2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
5 * T2 SDE: misc/tools-source/bash_profiler.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
8 * More information can be found in the files COPYING and README.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License. A copy of the
13 * GNU General Public License can be found in the file COPYING.
14 * --- T2-COPYRIGHT-NOTE-END ---
16 /* Bash (wallclock-time) profiler. Written by Clifford Wolf.
19 * gcc -shared -fPIC -Wall -o bash_profiler.so bash_profiler.c
20 * enable -f ./bash_profiler.so bprof
22 * bprof a start; idle_in_a; brof a stop
23 * bprof b start; idle_in_b; brof b stop
24 * bprof a start; idle_in_a; brof a stop
31 /* Some declarations copied from bash-2.05b headers */
35 typedef struct word_desc
{
40 typedef struct word_list
{
41 struct word_list
*next
;
45 typedef int sh_builtin_func_t(WORD_LIST
*);
47 #define BUILTIN_ENABLED 0x1
51 sh_builtin_func_t
*function
;
53 char * const *long_doc
;
54 const char *short_doc
;
59 /* my hellobash builtin */
70 return tv
.tv_sec
*1000 + tv
.tv_usec
/1000;
78 long long tv_sum
, tv_start
;
79 struct bprofent
*next
;
82 struct bprofent
*bprofent_list
= 0;
84 int bprof_builtin(WORD_LIST
*list
)
86 struct bprofent
*this = bprofent_list
;
89 if ( !list
|| !list
->next
) {
90 fprintf(stderr
, "Usage: bprof {id|all} {start|stop|print}\n");
94 name
= list
->word
->word
;
95 mode
= list
->next
->word
->word
;
97 if ( !strcmp(mode
, "print") && !strcmp(name
, "all") ) {
99 printf("%7d %7Ld %10.3f %s\n", this->count
, this->tv_sum
,
100 (float)this->tv_sum
/this->count
, this->id
);
107 if ( !strcmp(this->id
, name
) ) break;
112 this = calloc(1, sizeof(struct bprofent
));
113 this->id
= strdup(name
);
114 this->next
= bprofent_list
;
115 bprofent_list
= this;
118 if ( !strcmp(mode
, "start") ) {
119 this->tv_start
= mytime();
120 } else if ( !strcmp(mode
, "stop") ) {
121 this->tv_sum
+= mytime() - this->tv_start
;
123 } else if ( !strcmp(mode
, "print") ) {
124 printf("%7d %7Ld %10.3f %s\n", this->count
, this->tv_sum
,
125 (float)this->tv_sum
/this->count
, this->id
);
131 char *bprof_doc
[] = {
136 struct builtin bprof_struct
= {