* updated fastfetch (2.11.0 -> 2.18.1)
[t2sde.git] / misc / tools-source / bash_profiler.c
blob6314de355fc857dbef91f08fe4d26e108bc9d64c
1 /*
2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 *
5 * T2 SDE: misc/tools-source/bash_profiler.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 *
8 * More information can be found in the files COPYING and README.
9 *
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.
18 * Usage:
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
26 * bprof all print
27 * enable -d bprof
31 /* Some declarations copied from bash-2.05b headers */
33 #include <stdint.h>
35 typedef struct word_desc {
36 char *word;
37 int flags;
38 } WORD_DESC;
40 typedef struct word_list {
41 struct word_list *next;
42 WORD_DESC *word;
43 } WORD_LIST;
45 typedef int sh_builtin_func_t(WORD_LIST *);
47 #define BUILTIN_ENABLED 0x1
49 struct builtin {
50 char *name;
51 sh_builtin_func_t *function;
52 int flags;
53 char * const *long_doc;
54 const char *short_doc;
55 char *handle;
59 /* my hellobash builtin */
61 #include <stdio.h>
62 #include <string.h>
63 #include <stdlib.h>
64 #include <sys/time.h>
66 long long mytime()
68 struct timeval tv;
69 gettimeofday(&tv, 0);
70 return tv.tv_sec*1000 + tv.tv_usec/1000;
73 struct bprofent;
75 struct bprofent {
76 char *id;
77 int count;
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;
87 char *mode, *name;
89 if ( !list || !list->next ) {
90 fprintf(stderr, "Usage: bprof {id|all} {start|stop|print}\n");
91 return 1;
94 name = list->word->word;
95 mode = list->next->word->word;
97 if ( !strcmp(mode, "print") && !strcmp(name, "all") ) {
98 while ( this ) {
99 printf("%7d %7Ld %10.3f %s\n", this->count, this->tv_sum,
100 (float)this->tv_sum/this->count, this->id);
101 this = this->next;
103 return 0;
106 while ( this ) {
107 if ( !strcmp(this->id, name) ) break;
108 this = this->next;
111 if ( !this ) {
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;
122 this->count++;
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);
128 return 0;
131 char *bprof_doc[] = {
132 "bash profiler",
136 struct builtin bprof_struct = {
137 "bprof",
138 &bprof_builtin,
139 BUILTIN_ENABLED,
140 bprof_doc,
141 "bash profiler",