mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.3 KiB

  1. /* Bash (wallclock-time) profiler. Written by Clifford Wolf.
  2. *
  3. * Usage:
  4. * gcc -shared -fPIC -Wall -o bash_profiler.so bash_profiler.c
  5. * enable -f ./bash_profiler.so bprof
  6. *
  7. * bprof a start; idle_in_a; brof a stop
  8. * bprof b start; idle_in_b; brof b stop
  9. * bprof a start; idle_in_a; brof a stop
  10. *
  11. * bprof all print
  12. * enable -d bprof
  13. */
  14. /* Some declarations copied from bash-2.05b headers */
  15. #include <stdint.h>
  16. typedef struct word_desc {
  17. char *word;
  18. int flags;
  19. } WORD_DESC;
  20. typedef struct word_list {
  21. struct word_list *next;
  22. WORD_DESC *word;
  23. } WORD_LIST;
  24. typedef int sh_builtin_func_t(WORD_LIST *);
  25. #define BUILTIN_ENABLED 0x1
  26. struct builtin {
  27. char *name;
  28. sh_builtin_func_t *function;
  29. int flags;
  30. char * const *long_doc;
  31. const char *short_doc;
  32. char *handle;
  33. };
  34. /* my hellobash builtin */
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <sys/time.h>
  39. long long mytime()
  40. {
  41. struct timeval tv;
  42. gettimeofday(&tv, 0);
  43. return tv.tv_sec*1000 + tv.tv_usec/1000;
  44. }
  45. struct bprofent;
  46. struct bprofent {
  47. char *id;
  48. int count;
  49. long long tv_sum, tv_start;
  50. struct bprofent *next;
  51. };
  52. struct bprofent *bprofent_list = 0;
  53. int bprof_builtin(WORD_LIST *list)
  54. {
  55. struct bprofent *this = bprofent_list;
  56. char *mode, *name;
  57. if ( !list || !list->next ) {
  58. fprintf(stderr, "Usage: bprof {id|all} {start|stop|print}\n");
  59. return 1;
  60. }
  61. name = list->word->word;
  62. mode = list->next->word->word;
  63. if ( !strcmp(mode, "print") && !strcmp(name, "all") ) {
  64. while ( this ) {
  65. printf("%7d %7Ld %10.3f %s\n", this->count, this->tv_sum,
  66. (float)this->tv_sum/this->count, this->id);
  67. this = this->next;
  68. }
  69. return 0;
  70. }
  71. while ( this ) {
  72. if ( !strcmp(this->id, name) ) break;
  73. this = this->next;
  74. }
  75. if ( !this ) {
  76. this = calloc(1, sizeof(struct bprofent));
  77. this->id = strdup(name);
  78. this->next = bprofent_list;
  79. bprofent_list = this;
  80. }
  81. if ( !strcmp(mode, "start") ) {
  82. this->tv_start = mytime();
  83. } else if ( !strcmp(mode, "stop") ) {
  84. this->tv_sum += mytime() - this->tv_start;
  85. this->count++;
  86. } else if ( !strcmp(mode, "print") ) {
  87. printf("%7d %7Ld %10.3f %s\n", this->count, this->tv_sum,
  88. (float)this->tv_sum/this->count, this->id);
  89. }
  90. return 0;
  91. }
  92. char *bprof_doc[] = {
  93. "bash profiler",
  94. 0
  95. };
  96. struct builtin bprof_struct = {
  97. "bprof",
  98. &bprof_builtin,
  99. BUILTIN_ENABLED,
  100. bprof_doc,
  101. "bash profiler",
  102. 0
  103. };