stats.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /****************************************************************************
  2. *
  3. * MODULE: r.terraflow
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *****************************************************************************/
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <sys/types.h>
  21. #include <sys/time.h>
  22. #ifndef __MINGW32__
  23. #include <sys/resource.h>
  24. #endif
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include "stats.h"
  28. #ifdef HAS_UTRACE
  29. struct ut { char buf[8]; };
  30. void utrace __P((void *, int));
  31. #define UTRACE(s) \
  32. {struct ut u; strncpy(u.buf,s,8); utrace((void*)&u, sizeof u);}
  33. #else /* !HAS_UTRACE */
  34. #define UTRACE(s)
  35. #endif /* HAS_UTRACE */
  36. #undef UTRACE
  37. #ifdef UTRACE_ENABLE
  38. #define UTRACE(s) utrace(s)
  39. #else
  40. #define UTRACE(s)
  41. #endif
  42. void
  43. utrace(const char *s) {
  44. void *p;
  45. int len = strlen(s);
  46. assert(len < 80);
  47. /* cerr << "UT " << len << endl; */
  48. p = malloc(0);
  49. /* assert(p); */
  50. free(p);
  51. p = malloc(len);
  52. /* assert(p); */
  53. free(p);
  54. for(int i=0; i<len; i++) {
  55. p = malloc(s[i]);
  56. /* assert(p); */
  57. free(p);
  58. }
  59. }
  60. int
  61. noclobberFile(char *fname) {
  62. int fd=-1;
  63. while(fd<0) {
  64. fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
  65. if(fd < 0) {
  66. if(errno != EEXIST) {
  67. perror(fname);
  68. exit(1);
  69. } else { /* file exists */
  70. char buf[BUFSIZ];
  71. fprintf(stderr, "file %s exists - renaming.\n", fname);
  72. sprintf(buf, "%s.old", fname);
  73. if(rename(fname, buf) != 0) {
  74. perror(fname);
  75. exit(1);
  76. }
  77. }
  78. }
  79. }
  80. return fd;
  81. }
  82. char*
  83. noclobberFileName(char *fname) {
  84. int fd;
  85. fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
  86. if(fd < 0) {
  87. if(errno != EEXIST) {
  88. perror(fname);
  89. exit(1);
  90. } else { /* file exists */
  91. char buf[BUFSIZ];
  92. fprintf(stderr, "file %s exists - renaming.\n", fname);
  93. sprintf(buf, "%s.old", fname);
  94. if(rename(fname, buf) != 0) {
  95. perror(fname);
  96. exit(1);
  97. }
  98. close(fd);
  99. }
  100. }
  101. return fname;
  102. }
  103. /* ********************************************************************** */
  104. statsRecorder::statsRecorder(char *fname) : ofstream(noclobberFileName(fname)){
  105. //note: in the new version of gcc there is not constructor for
  106. //ofstream that takes an fd; wrote another noclobber() function that
  107. //closes fd and returns the name;
  108. rt_start(tm);
  109. #ifndef __MINGW32__
  110. bss = sbrk(0);
  111. #endif
  112. char buf[BUFSIZ];
  113. *this << freeMem(buf) << endl;
  114. }
  115. /* ********************************************************************** */
  116. long
  117. statsRecorder::freeMem() {
  118. #ifdef __MINGW32__
  119. return -1;
  120. #else
  121. struct rlimit rlim;
  122. if (getrlimit(RLIMIT_DATA, &rlim) == -1) {
  123. perror("getrlimit: ");
  124. return -1;
  125. }
  126. /* printf("getrlimit returns: %d \n", rlim.rlim_cur); */
  127. if (rlim.rlim_cur == RLIM_INFINITY) {
  128. /* printf("rlim is infinity\n"); */
  129. /* should fix this */
  130. return -1;
  131. }
  132. long freeMem = rlim.rlim_cur - ((char*)sbrk(0)-(char*)bss);
  133. return freeMem;
  134. #endif /* __MINGW32__ */
  135. }
  136. char *
  137. statsRecorder::freeMem(char *buf) {
  138. char buf2[BUFSIZ];
  139. sprintf(buf, "Free Memory=%s", formatNumber(buf2, freeMem()));
  140. return buf;
  141. }
  142. /* ********************************************************************** */
  143. char *
  144. statsRecorder::timestamp() {
  145. static char buf[BUFSIZ];
  146. rt_stop(tm);
  147. sprintf(buf, "[%.1f] ", rt_seconds(tm));
  148. return buf;
  149. }
  150. void
  151. statsRecorder::timestamp(const char *s) {
  152. *this << timestamp() << s << endl;
  153. }
  154. void
  155. statsRecorder::comment(const char *s, const int verbose) {
  156. *this << timestamp() << s << endl;
  157. if (verbose) {
  158. cout << s << endl;
  159. }
  160. UTRACE(s);
  161. cout.flush();
  162. }
  163. void
  164. statsRecorder::comment(const char *s1, const char *s2) {
  165. char buf[BUFSIZ];
  166. sprintf(buf, "%s%s", s1, s2);
  167. comment(buf);
  168. }
  169. void
  170. statsRecorder::comment(const int n) {
  171. char buf[BUFSIZ];
  172. sprintf(buf, "%d", n);
  173. comment(buf);
  174. }
  175. #if __FreeBSD__ && __i386__
  176. #define LDFMT "%qd"
  177. #else
  178. #if __linux__
  179. #define LDFMT "%lld"
  180. #else
  181. #define LDFMT "%ld"
  182. #endif
  183. #endif
  184. char *
  185. formatNumber(char *buf, off_t val) {
  186. if(val > (1<<30)) {
  187. sprintf(buf, "%.2fG (" LDFMT ")", (double)val/(1<<30), val);
  188. } else if(val > (1<<20)) {
  189. sprintf(buf, "%.2fM (" LDFMT ")", (double)val/(1<<20), val);
  190. } else if(val > (1<<10)) {
  191. sprintf(buf, "%.2fK (" LDFMT ")", (double)val/(1<<10), val);
  192. } else {
  193. sprintf(buf, LDFMT, val);
  194. }
  195. return buf;
  196. }
  197. void
  198. statsRecorder::recordTime(const char *label, long secs) {
  199. *this << timestamp() << "TIME " << label << ": " << secs << " secs" << endl;
  200. this->flush();
  201. UTRACE(label);
  202. }
  203. void
  204. statsRecorder::recordTime(const char *label, Rtimer rt) {
  205. char buf[BUFSIZ];
  206. *this << timestamp() << "TIME " << label << ": ";
  207. *this << rt_sprint(buf, rt) << endl;
  208. this->flush();
  209. UTRACE(label);
  210. }
  211. void
  212. statsRecorder::recordLength(const char *label, off_t len, int siz,
  213. char *sname) {
  214. UTRACE(label);
  215. UTRACE(sname);
  216. char lenstr[100];
  217. char suffix[100]="";
  218. if(siz) {
  219. formatNumber(suffix, len*siz);
  220. strcat(suffix, " bytes");
  221. }
  222. formatNumber(lenstr, len);
  223. *this << timestamp() << "LEN " << label << ": " << lenstr << " elts "
  224. << suffix;
  225. if(sname) *this << " " << sname;
  226. *this << endl;
  227. this->flush();
  228. }