"Fossies" - the Fresh Open Source Software Archive 
Member "bonnie++-2.00a/zcav.cpp" (23 Nov 2012, 6063 Bytes) of package /linux/privat/bonnie++-2.00a.tgz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "zcav.cpp" see the
Fossies "Dox" file reference documentation and the last
Fossies "Diffs" side-by-side code changes report:
1.03e_vs_1.04.
1 #include "port.h"
2 #include <unistd.h>
3
4 #include "zcav_io.h"
5 #include "thread.h"
6
7 #include <cstdlib>
8 #include <cstring>
9
10 #define TOO_MANY_LOOPS 100
11
12 void usage()
13 {
14 fprintf(stderr
15 , "Usage: zcav [-b block-size[:chunk-size]] [-c count]\n"
16 " [-r [start offset:]end offset] [-w]\n"
17 " [-u uid-to-use:gid-to-use] [-g gid-to-use]\n"
18 #ifdef _LARGEFILE64_SOURCE
19 " [-s skip rate]\n"
20 #endif
21 " [-l log-file] [-f] file-name\n"
22 " [-l log-file [-f] file-name]...\n"
23 "\n"
24 "File name of \"-\" means standard input\n"
25 "Count is the number of times to read the data (default 1).\n"
26 "Max size is the amount of data to read from each device.\n"
27 "\n"
28 "Version: " BON_VERSION "\n");
29 exit(1);
30 }
31
32 class MultiZcav : public Thread
33 {
34 public:
35 MultiZcav();
36 MultiZcav(int threadNum, const MultiZcav *parent);
37 virtual ~MultiZcav();
38
39 virtual int action(PVOID param);
40
41 int runit();
42
43 void setFileLogNames(const char *file, const char *log)
44 {
45 m_fileNames.push_back(file);
46 m_logNames.push_back(log);
47 m_readers->push_back(new ZcavRead);
48 }
49
50 void setSizes(int block_size, int chunk_size)
51 {
52 m_block_size = block_size;
53 m_chunk_size = chunk_size;
54 if(m_block_size < 1 || m_chunk_size < 1 || m_chunk_size > m_block_size)
55 usage();
56 }
57
58 void setWrite(int do_write)
59 {
60 m_do_write = do_write;
61 }
62
63 void setLoops(int max_loops)
64 {
65 m_max_loops = max_loops;
66 if(max_loops < 1 || max_loops > TOO_MANY_LOOPS)
67 usage();
68 }
69
70 void setMaxSize(int max_size)
71 {
72 m_max_size = max_size;
73 if(max_size < 1)
74 usage();
75 }
76
77 void setStartOffset(int start_offset)
78 {
79 m_start_offset = start_offset;
80 if(start_offset < 1)
81 usage();
82 }
83
84 void setSkipRate(int skip_rate)
85 {
86 m_skip_rate = skip_rate;
87 if(skip_rate < 2 || skip_rate > 20)
88 usage();
89 }
90
91 private:
92 virtual Thread *newThread(int threadNum)
93 { return new MultiZcav(threadNum, this); }
94
95 vector<const char *> m_fileNames, m_logNames;
96 vector<ZcavRead *> *m_readers;
97
98 int m_block_size, m_max_loops, m_max_size, m_start_offset, m_skip_rate;
99 int m_chunk_size, m_do_write;
100
101 MultiZcav(const MultiZcav &m);
102 MultiZcav & operator =(const MultiZcav &m);
103 };
104
105 MultiZcav::MultiZcav()
106 {
107 m_block_size = DEFAULT_BLOCK_SIZE;
108 m_max_loops = 1;
109 m_max_size = 0;
110 m_start_offset = 0;
111 m_skip_rate = 1;
112 m_chunk_size = DEFAULT_CHUNK_SIZE;
113 m_do_write = 0;
114 m_readers = new vector<ZcavRead *>;
115 }
116
117 MultiZcav::MultiZcav(int threadNum, const MultiZcav *parent)
118 : Thread(threadNum, parent)
119 , m_readers(parent->m_readers)
120 , m_block_size(parent->m_block_size)
121 , m_max_loops(parent->m_max_loops)
122 , m_max_size(parent->m_max_size)
123 , m_start_offset(parent->m_start_offset)
124 , m_skip_rate(parent->m_skip_rate)
125 {
126 }
127
128 int MultiZcav::action(PVOID)
129 {
130 ZcavRead *zc = (*m_readers)[getThreadNum() - 1];
131 int rc = zc->Read(m_max_loops, m_max_size / m_block_size, m_write, m_skip_rate, m_start_offset / m_block_size);
132 zc->Close();
133 return rc;
134 }
135
136 MultiZcav::~MultiZcav()
137 {
138 if(getThreadNum() < 1)
139 {
140 while(m_readers->size())
141 {
142 delete m_readers->back();
143 m_readers->pop_back();
144 }
145 delete m_readers;
146 }
147 }
148
149 int MultiZcav::runit()
150 {
151 unsigned int i;
152 unsigned int num_threads = m_fileNames.size();
153 if(num_threads < 1)
154 usage();
155 for(i = 0; i < num_threads; i++)
156 {
157 if((*m_readers)[i]->Open(NULL, m_block_size, m_fileNames[i], m_logNames[i], m_chunk_size, m_do_write))
158 {
159 return 1;
160 }
161 }
162 go(NULL, num_threads);
163 int res = 0;
164 while(num_threads)
165 {
166 char c = 0;
167 if(Read(&c, 1, 0) != 1)
168 printf("can't read!\n");
169 num_threads--;
170 if(c > res)
171 res = c;
172 }
173 return res;
174 }
175
176 int main(int argc, char *argv[])
177 {
178 MultiZcav mz;
179
180 if(argc < 2)
181 usage();
182
183 char *userName = NULL, *groupName = NULL;
184 int c;
185 int do_write = 0;
186 const char *log = "-";
187 const char *file = "";
188 while(-1 != (c = getopt(argc, argv, "-c:b:f:l:r:w"
189 #ifdef _LARGEFILE64_SOURCE
190 "s:"
191 #endif
192 "u:g:")) )
193 {
194 switch(char(c))
195 {
196 case 'b':
197 {
198 int block_size, chunk_size;
199 int rc = sscanf(optarg, "%d:%d", &block_size, &chunk_size);
200 if(rc == 1)
201 chunk_size = DEFAULT_CHUNK_SIZE;
202 else if(rc != 2)
203 usage();
204 mz.setSizes(block_size, chunk_size);
205 }
206 break;
207 case 'c':
208 mz.setLoops(atoi(optarg));
209 break;
210 case 'l':
211 log = optarg;
212 break;
213 case 'r':
214 {
215 int a, b, rc;
216 rc = sscanf(optarg, "%d:%d", &a, &b);
217 if(rc == 0)
218 usage();
219 if(rc == 1)
220 mz.setMaxSize(a);
221 else
222 {
223 mz.setStartOffset(a);
224 mz.setMaxSize(b);
225 }
226 }
227 break;
228 #ifdef _LARGEFILE64_SOURCE
229 case 's':
230 mz.setSkipRate(atoi(optarg));
231 break;
232 #endif
233 case 'g':
234 if(groupName)
235 usage();
236 groupName = optarg;
237 break;
238 case 'u':
239 {
240 if(userName)
241 usage();
242 userName = strdup(optarg);
243 int i;
244 for(i = 0; userName[i] && userName[i] != ':'; i++) {}
245
246 if(userName[i] == ':')
247 {
248 if(groupName)
249 usage();
250 userName[i] = '\0';
251 groupName = &userName[i + 1];
252 }
253 }
254 break;
255 case 'w':
256 mz.setWrite(1);
257 do_write = 1;
258 break;
259 case 'f':
260 case char(1):
261 mz.setFileLogNames(optarg, log);
262 file = optarg;
263 log = "-";
264 break;
265 default:
266 usage();
267 }
268 }
269
270 if(userName || groupName)
271 {
272 if(bon_setugid(userName, groupName, false))
273 return 1;
274 if(userName)
275 free(userName);
276 }
277
278 if(do_write)
279 {
280 fprintf(stderr, "Warning, writing to %s in 5 seconds.\n", file);
281 sleep(5);
282 }
283 int rc = mz.runit();
284 sleep(2); // time for all threads to complete
285 return rc;
286 }
287