stats.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. }
  108. /* ********************************************************************** */
  109. char *
  110. statsRecorder::timestamp() {
  111. static char buf[BUFSIZ];
  112. rt_stop(tm);
  113. sprintf(buf, "[%.1f] ", rt_seconds(tm));
  114. return buf;
  115. }
  116. void
  117. statsRecorder::timestamp(const char *s) {
  118. *this << timestamp() << s << endl;
  119. }
  120. void
  121. statsRecorder::comment(const char *s, const int verbose) {
  122. *this << timestamp() << s << endl;
  123. if (verbose) {
  124. cout << s << endl;
  125. }
  126. UTRACE(s);
  127. cout.flush();
  128. }
  129. void
  130. statsRecorder::comment(const char *s1, const char *s2) {
  131. char buf[BUFSIZ];
  132. sprintf(buf, "%s%s", s1, s2);
  133. comment(buf);
  134. }
  135. void
  136. statsRecorder::comment(const int n) {
  137. char buf[BUFSIZ];
  138. sprintf(buf, "%d", n);
  139. comment(buf);
  140. }
  141. char *
  142. formatNumber(char *buf, off_t val) {
  143. if(val > (1<<30)) {
  144. sprintf(buf, "%.2fG (%" PRI_OFF_T ")", (double)val/(1<<30), val);
  145. } else if(val > (1<<20)) {
  146. sprintf(buf, "%.2fM (%" PRI_OFF_T ")", (double)val/(1<<20), val);
  147. } else if(val > (1<<10)) {
  148. sprintf(buf, "%.2fK (%" PRI_OFF_T ")", (double)val/(1<<10), val);
  149. } else {
  150. sprintf(buf, "%" PRI_OFF_T, val);
  151. }
  152. return buf;
  153. }
  154. void
  155. statsRecorder::recordTime(const char *label, long secs) {
  156. *this << timestamp() << "TIME " << label << ": " << secs << " secs" << endl;
  157. this->flush();
  158. UTRACE(label);
  159. }
  160. void
  161. statsRecorder::recordTime(const char *label, Rtimer rt) {
  162. char buf[BUFSIZ];
  163. *this << timestamp() << "TIME " << label << ": ";
  164. *this << rt_sprint(buf, rt) << endl;
  165. this->flush();
  166. UTRACE(label);
  167. }
  168. void
  169. statsRecorder::recordLength(const char *label, off_t len, int siz,
  170. char *sname) {
  171. UTRACE(label);
  172. UTRACE(sname);
  173. char lenstr[100];
  174. char suffix[100]="";
  175. if(siz) {
  176. formatNumber(suffix, len*siz);
  177. strcat(suffix, " bytes");
  178. }
  179. formatNumber(lenstr, len);
  180. *this << timestamp() << "LEN " << label << ": " << lenstr << " elts "
  181. << suffix;
  182. if(sname) *this << " " << sname;
  183. *this << endl;
  184. this->flush();
  185. }