PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/assn1/stats.h

https://github.com/Mercidian/EECE476_A1
C Header | 332 lines | 188 code | 33 blank | 111 comment | 0 complexity | 30000cf4b8e4b170a5ce14b140fe90a1 MD5 | raw file
  1. /* stats.h - statistical package interfaces */
  2. /* SimpleScalar(TM) Tool Suite
  3. * Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
  4. * All Rights Reserved.
  5. *
  6. * THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR,
  7. * YOU ARE AGREEING TO THESE TERMS AND CONDITIONS.
  8. *
  9. * No portion of this work may be used by any commercial entity, or for any
  10. * commercial purpose, without the prior, written permission of SimpleScalar,
  11. * LLC (info@simplescalar.com). Nonprofit and noncommercial use is permitted
  12. * as described below.
  13. *
  14. * 1. SimpleScalar is provided AS IS, with no warranty of any kind, express
  15. * or implied. The user of the program accepts full responsibility for the
  16. * application of the program and the use of any results.
  17. *
  18. * 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be
  19. * downloaded, compiled, executed, copied, and modified solely for nonprofit,
  20. * educational, noncommercial research, and noncommercial scholarship
  21. * purposes provided that this notice in its entirety accompanies all copies.
  22. * Copies of the modified software can be delivered to persons who use it
  23. * solely for nonprofit, educational, noncommercial research, and
  24. * noncommercial scholarship purposes provided that this notice in its
  25. * entirety accompanies all copies.
  26. *
  27. * 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY
  28. * PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC (info@simplescalar.com).
  29. *
  30. * 4. No nonprofit user may place any restrictions on the use of this software,
  31. * including as modified by the user, by any other authorized user.
  32. *
  33. * 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar
  34. * in compiled or executable form as set forth in Section 2, provided that
  35. * either: (A) it is accompanied by the corresponding machine-readable source
  36. * code, or (B) it is accompanied by a written offer, with no time limit, to
  37. * give anyone a machine-readable copy of the corresponding source code in
  38. * return for reimbursement of the cost of distribution. This written offer
  39. * must permit verbatim duplication by anyone, or (C) it is distributed by
  40. * someone who received only the executable form, and is accompanied by a
  41. * copy of the written offer of source code.
  42. *
  43. * 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is
  44. * currently maintained by SimpleScalar LLC (info@simplescalar.com). US Mail:
  45. * 2395 Timbercrest Court, Ann Arbor, MI 48105.
  46. *
  47. * Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
  48. */
  49. #ifndef STAT_H
  50. #define STAT_H
  51. #include <stdio.h>
  52. #include "host.h"
  53. #include "machine.h"
  54. #include "eval.h"
  55. /*
  56. * The stats package is a uniform module for handling statistical variables,
  57. * including counters, distributions, and expressions. The user must first
  58. * create a stats database using stat_new(), then statical counters are added
  59. * to the database using the *_reg_*() functions. Interfaces are included to
  60. * allocate and manipulate distributions (histograms) and general expression
  61. * of other statistical variables constants. Statistical variables can be
  62. * located by name using stat_find_stat(). And, statistics can be print in
  63. * a highly standardized and stylized fashion using stat_print_stats().
  64. */
  65. /* stat variable classes */
  66. enum stat_class_t {
  67. sc_int = 0, /* integer stat */
  68. sc_uint, /* unsigned integer stat */
  69. #ifdef HOST_HAS_QWORD
  70. sc_qword, /* qword integer stat */
  71. sc_sqword, /* signed qword integer stat */
  72. #endif /* HOST_HAS_QWORD */
  73. sc_float, /* single-precision FP stat */
  74. sc_double, /* double-precision FP stat */
  75. sc_dist, /* array distribution stat */
  76. sc_sdist, /* sparse array distribution stat */
  77. sc_formula, /* stat expression formula */
  78. sc_NUM
  79. };
  80. /* sparse array distributions are implemented with a hash table */
  81. #define HTAB_SZ 1024
  82. #define HTAB_HASH(I) ((((I) >> 8) ^ (I)) & (HTAB_SZ - 1))
  83. /* hash table bucket definition */
  84. struct bucket_t {
  85. struct bucket_t *next; /* pointer to the next bucket */
  86. md_addr_t index; /* bucket index - as large as an addr */
  87. unsigned int count; /* bucket count */
  88. };
  89. /* forward declaration */
  90. struct stat_stat_t;
  91. /* enable distribution components: index, count, probability, cumulative */
  92. #define PF_COUNT 0x0001
  93. #define PF_PDF 0x0002
  94. #define PF_CDF 0x0004
  95. #define PF_ALL (PF_COUNT|PF_PDF|PF_CDF)
  96. /* user-defined print function, if this option is selected, a function of this
  97. form is called for each bucket in the distribution, in ascending index
  98. order */
  99. typedef void
  100. (*print_fn_t)(struct stat_stat_t *stat, /* the stat variable being printed */
  101. md_addr_t index, /* entry index to print */
  102. int count, /* entry count */
  103. double sum, /* cumulative sum */
  104. double total); /* total count for distribution */
  105. /* statistical variable definition */
  106. struct stat_stat_t {
  107. struct stat_stat_t *next; /* pointer to next stat in database list */
  108. char *name; /* stat name */
  109. char *desc; /* stat description */
  110. char *format; /* stat output print format */
  111. enum stat_class_t sc; /* stat class */
  112. union stat_variant_t {
  113. /* sc == sc_int */
  114. struct stat_for_int_t {
  115. int *var; /* integer stat variable */
  116. int init_val; /* initial integer value */
  117. } for_int;
  118. /* sc == sc_uint */
  119. struct stat_for_uint_t {
  120. unsigned int *var; /* unsigned integer stat variable */
  121. unsigned int init_val; /* initial unsigned integer value */
  122. } for_uint;
  123. #ifdef HOST_HAS_QWORD
  124. /* sc == sc_qword */
  125. struct stat_for_qword_t {
  126. qword_t *var; /* qword integer stat variable */
  127. qword_t init_val; /* qword integer value */
  128. } for_qword;
  129. /* sc == sc_sqword */
  130. struct stat_for_sqword_t {
  131. sqword_t *var; /* signed qword integer stat variable */
  132. sqword_t init_val; /* signed qword integer value */
  133. } for_sqword;
  134. #endif /* HOST_HAS_QWORD */
  135. /* sc == sc_float */
  136. struct stat_for_float_t {
  137. float *var; /* float stat variable */
  138. float init_val; /* initial float value */
  139. } for_float;
  140. /* sc == sc_double */
  141. struct stat_for_double_t {
  142. double *var; /* double stat variable */
  143. double init_val; /* initial double value */
  144. } for_double;
  145. /* sc == sc_dist */
  146. struct stat_for_dist_t {
  147. unsigned int init_val; /* initial dist value */
  148. unsigned int *arr; /* non-sparse array pointer */
  149. unsigned int arr_sz; /* array size */
  150. unsigned int bucket_sz; /* array bucket size */
  151. int pf; /* printables */
  152. char **imap; /* index -> string map */
  153. print_fn_t print_fn; /* optional user-specified print fn */
  154. unsigned int overflows; /* total overflows in stat_add_samples() */
  155. } for_dist;
  156. /* sc == sc_sdist */
  157. struct stat_for_sdist_t {
  158. unsigned int init_val; /* initial dist value */
  159. struct bucket_t **sarr; /* sparse array pointer */
  160. int pf; /* printables */
  161. print_fn_t print_fn; /* optional user-specified print fn */
  162. } for_sdist;
  163. /* sc == sc_formula */
  164. struct stat_for_formula_t {
  165. char *formula; /* stat formula, see eval.h for format */
  166. } for_formula;
  167. } variant;
  168. };
  169. /* statistical database */
  170. struct stat_sdb_t {
  171. struct stat_stat_t *stats; /* list of stats in database */
  172. struct eval_state_t *evaluator; /* an expression evaluator */
  173. };
  174. /* evaluate a stat as an expression */
  175. struct eval_value_t
  176. stat_eval_ident(struct eval_state_t *es);/* expression stat to evaluate */
  177. /* create a new stats database */
  178. struct stat_sdb_t *stat_new(void);
  179. /* delete a stats database */
  180. void
  181. stat_delete(struct stat_sdb_t *sdb); /* stats database */
  182. /* register an integer statistical variable */
  183. struct stat_stat_t *
  184. stat_reg_int(struct stat_sdb_t *sdb, /* stat database */
  185. char *name, /* stat variable name */
  186. char *desc, /* stat variable description */
  187. int *var, /* stat variable */
  188. int init_val, /* stat variable initial value */
  189. char *format); /* optional variable output format */
  190. /* register an unsigned integer statistical variable */
  191. struct stat_stat_t *
  192. stat_reg_uint(struct stat_sdb_t *sdb, /* stat database */
  193. char *name, /* stat variable name */
  194. char *desc, /* stat variable description */
  195. unsigned int *var, /* stat variable */
  196. unsigned int init_val, /* stat variable initial value */
  197. char *format); /* optional variable output format */
  198. #ifdef HOST_HAS_QWORD
  199. /* register a qword integer statistical variable */
  200. struct stat_stat_t *
  201. stat_reg_qword(struct stat_sdb_t *sdb, /* stat database */
  202. char *name, /* stat variable name */
  203. char *desc, /* stat variable description */
  204. qword_t *var, /* stat variable */
  205. qword_t init_val, /* stat variable initial value */
  206. char *format); /* optional variable output format */
  207. /* register a signed qword integer statistical variable */
  208. struct stat_stat_t *
  209. stat_reg_sqword(struct stat_sdb_t *sdb, /* stat database */
  210. char *name, /* stat variable name */
  211. char *desc, /* stat variable description */
  212. sqword_t *var, /* stat variable */
  213. sqword_t init_val, /* stat variable initial value */
  214. char *format); /* optional variable output format */
  215. #endif /* HOST_HAS_QWORD */
  216. /* register a float statistical variable */
  217. struct stat_stat_t *
  218. stat_reg_float(struct stat_sdb_t *sdb, /* stat database */
  219. char *name, /* stat variable name */
  220. char *desc, /* stat variable description */
  221. float *var, /* stat variable */
  222. float init_val, /* stat variable initial value */
  223. char *format); /* optional variable output format */
  224. /* register a double statistical variable */
  225. struct stat_stat_t *
  226. stat_reg_double(struct stat_sdb_t *sdb, /* stat database */
  227. char *name, /* stat variable name */
  228. char *desc, /* stat variable description */
  229. double *var, /* stat variable */
  230. double init_val, /* stat variable initial value */
  231. char *format); /* optional variable output format */
  232. /* create an array distribution (w/ fixed size buckets) in stat database SDB,
  233. the array distribution has ARR_SZ buckets with BUCKET_SZ indicies in each
  234. bucked, PF specifies the distribution components to print for optional
  235. format FORMAT; the indicies may be optionally replaced with the strings
  236. from IMAP, or the entire distribution can be printed with the optional
  237. user-specified print function PRINT_FN */
  238. struct stat_stat_t *
  239. stat_reg_dist(struct stat_sdb_t *sdb, /* stat database */
  240. char *name, /* stat variable name */
  241. char *desc, /* stat variable description */
  242. unsigned int init_val, /* dist initial value */
  243. unsigned int arr_sz, /* array size */
  244. unsigned int bucket_sz, /* array bucket size */
  245. int pf, /* print format, use PF_* defs */
  246. char *format, /* optional variable output format */
  247. char **imap, /* optional index -> string map */
  248. print_fn_t print_fn); /* optional user print function */
  249. /* create a sparse array distribution in stat database SDB, while the sparse
  250. array consumes more memory per bucket than an array distribution, it can
  251. efficiently map any number of indicies from 0 to 2^32-1, PF specifies the
  252. distribution components to print for optional format FORMAT; the indicies
  253. may be optionally replaced with the strings from IMAP, or the entire
  254. distribution can be printed with the optional user-specified print function
  255. PRINT_FN */
  256. struct stat_stat_t *
  257. stat_reg_sdist(struct stat_sdb_t *sdb, /* stat database */
  258. char *name, /* stat variable name */
  259. char *desc, /* stat variable description */
  260. unsigned int init_val, /* dist initial value */
  261. int pf, /* print format, use PF_* defs */
  262. char *format, /* optional variable output format */
  263. print_fn_t print_fn); /* optional user print function */
  264. /* add NSAMPLES to array or sparse array distribution STAT */
  265. void
  266. stat_add_samples(struct stat_stat_t *stat,/* stat database */
  267. md_addr_t index, /* distribution index of samples */
  268. int nsamples); /* number of samples to add to dist */
  269. /* add a single sample to array or sparse array distribution STAT */
  270. void
  271. stat_add_sample(struct stat_stat_t *stat,/* stat variable */
  272. md_addr_t index); /* index of sample */
  273. /* register a double statistical formula, the formula is evaluated when the
  274. statistic is printed, the formula expression may reference any registered
  275. statistical variable and, in addition, the standard operators '(', ')', '+',
  276. '-', '*', and '/', and literal (i.e., C-format decimal, hexidecimal, and
  277. octal) constants are also supported; NOTE: all terms are immediately
  278. converted to double values and the result is a double value, see eval.h
  279. for more information on formulas */
  280. struct stat_stat_t *
  281. stat_reg_formula(struct stat_sdb_t *sdb,/* stat database */
  282. char *name, /* stat variable name */
  283. char *desc, /* stat variable description */
  284. char *formula, /* formula expression */
  285. char *format); /* optional variable output format */
  286. /* print the value of stat variable STAT */
  287. void
  288. stat_print_stat(struct stat_sdb_t *sdb, /* stat database */
  289. struct stat_stat_t *stat,/* stat variable */
  290. FILE *fd); /* output stream */
  291. /* print the value of all stat variables in stat database SDB */
  292. void
  293. stat_print_stats(struct stat_sdb_t *sdb,/* stat database */
  294. FILE *fd); /* output stream */
  295. /* find a stat variable, returns NULL if it is not found */
  296. struct stat_stat_t *
  297. stat_find_stat(struct stat_sdb_t *sdb, /* stat database */
  298. char *stat_name); /* stat name */
  299. #endif /* STAT_H */