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