stats.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. G_debug(1, "file %s exists - renaming.\n", fname);
  72. sprintf(buf, "%s.old", fname);
  73. if(rename(fname, buf) != 0) {
  74. G_fatal_error("%s", fname);
  75. }
  76. }
  77. }
  78. }
  79. return fd;
  80. }
  81. char*
  82. noclobberFileName(char *fname) {
  83. int fd;
  84. fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
  85. if(fd < 0) {
  86. if(errno != EEXIST) {
  87. perror(fname);
  88. exit(1);
  89. } else { /* file exists */
  90. char buf[BUFSIZ];
  91. G_debug(1, "file %s exists - renaming.\n", fname);
  92. sprintf(buf, "%s.old", fname);
  93. if(rename(fname, buf) != 0) {
  94. G_fatal_error("%s", fname);
  95. }
  96. close(fd);
  97. }
  98. }
  99. return fname;
  100. }
  101. /* ********************************************************************** */
  102. statsRecorder::statsRecorder(char *fname) : ofstream(noclobberFileName(fname)){
  103. //note: in the new version of gcc there is not constructor for
  104. //ofstream that takes an fd; wrote another noclobber() function that
  105. //closes fd and returns the name;
  106. rt_start(tm);
  107. #ifndef __MINGW32__
  108. bss = sbrk(0);
  109. #endif
  110. char buf[BUFSIZ];
  111. *this << freeMem(buf) << endl;
  112. }
  113. /* ********************************************************************** */
  114. long
  115. statsRecorder::freeMem() {
  116. #ifdef __MINGW32__
  117. return -1;
  118. #else
  119. struct rlimit rlim;
  120. if (getrlimit(RLIMIT_DATA, &rlim) == -1) {
  121. perror("getrlimit: ");
  122. return -1;
  123. }
  124. /* printf("getrlimit returns: %d \n", rlim.rlim_cur); */
  125. if (rlim.rlim_cur == RLIM_INFINITY) {
  126. /* printf("rlim is infinity\n"); */
  127. /* should fix this */
  128. return -1;
  129. }
  130. long freeMem = rlim.rlim_cur - ((char*)sbrk(0)-(char*)bss);
  131. return freeMem;
  132. #endif /* __MINGW32__ */
  133. }
  134. char *
  135. statsRecorder::freeMem(char *buf) {
  136. char buf2[BUFSIZ];
  137. sprintf(buf, "Free Memory=%s", formatNumber(buf2, freeMem()));
  138. return buf;
  139. }
  140. /* ********************************************************************** */
  141. char *
  142. statsRecorder::timestamp() {
  143. static char buf[BUFSIZ];
  144. rt_stop(tm);
  145. sprintf(buf, "[%.1f] ", rt_seconds(tm));
  146. return buf;
  147. }
  148. void
  149. statsRecorder::timestamp(const char *s) {
  150. *this << timestamp() << s << endl;
  151. }
  152. void
  153. statsRecorder::comment(const char *s, const int verbose) {
  154. *this << timestamp() << s << endl;
  155. if (verbose) {
  156. cout << s << endl;
  157. }
  158. UTRACE(s);
  159. cout.flush();
  160. }
  161. void
  162. statsRecorder::comment(const char *s1, const char *s2) {
  163. char buf[BUFSIZ];
  164. sprintf(buf, "%s%s", s1, s2);
  165. comment(buf);
  166. }
  167. void
  168. statsRecorder::comment(const int n) {
  169. char buf[BUFSIZ];
  170. sprintf(buf, "%d", n);
  171. comment(buf);
  172. }
  173. #if __FreeBSD__ && __i386__
  174. #define LDFMT "%qd"
  175. #else
  176. #if __linux__
  177. #define LDFMT "%lld"
  178. #else
  179. #define LDFMT "%ld"
  180. #endif
  181. #endif
  182. char *
  183. formatNumber(char *buf, off_t val) {
  184. if(val > (1<<30)) {
  185. sprintf(buf, "%.2fG (" LDFMT ")", (double)val/(1<<30), val);
  186. } else if(val > (1<<20)) {
  187. sprintf(buf, "%.2fM (" LDFMT ")", (double)val/(1<<20), val);
  188. } else if(val > (1<<10)) {
  189. sprintf(buf, "%.2fK (" LDFMT ")", (double)val/(1<<10), val);
  190. } else {
  191. sprintf(buf, LDFMT, val);
  192. }
  193. return buf;
  194. }
  195. void
  196. statsRecorder::recordTime(const char *label, long secs) {
  197. *this << timestamp() << "TIME " << label << ": " << secs << " secs" << endl;
  198. this->flush();
  199. UTRACE(label);
  200. }
  201. void
  202. statsRecorder::recordTime(const char *label, Rtimer rt) {
  203. char buf[BUFSIZ];
  204. *this << timestamp() << "TIME " << label << ": ";
  205. *this << rt_sprint(buf, rt) << endl;
  206. this->flush();
  207. UTRACE(label);
  208. }
  209. void
  210. statsRecorder::recordLength(const char *label, off_t len, int siz,
  211. char *sname) {
  212. UTRACE(label);
  213. UTRACE(sname);
  214. char lenstr[100];
  215. char suffix[100]="";
  216. if(siz) {
  217. formatNumber(suffix, len*siz);
  218. strcat(suffix, " bytes");
  219. }
  220. formatNumber(lenstr, len);
  221. *this << timestamp() << "LEN " << label << ": " << lenstr << " elts "
  222. << suffix;
  223. if(sname) *this << " " << sname;
  224. *this << endl;
  225. this->flush();
  226. }