]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bbatch.c
5d2d144b05493ed923bf514f2defab10cfd902c9
[bacula/bacula] / bacula / src / tools / bbatch.c
1 /*
2  *
3  *  Program to test batch mode
4  *
5  *   Eric Bollengier, March 2007
6  *
7  *
8  *   Version $Id$
9  */
10 /*
11    Bacula® - The Network Backup Solution
12
13    Copyright (C) 2001-2006 Free Software Foundation Europe e.V.
14
15    The main author of Bacula is Kern Sibbald, with contributions from
16    many others, a complete list can be found in the file AUTHORS.
17    This program is Free Software; you can redistribute it and/or
18    modify it under the terms of version three of the GNU Affero General Public
19    License as published by the Free Software Foundation and included
20    in the file LICENSE.
21
22    This program is distributed in the hope that it will be useful, but
23    WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU Affero General Public License
28    along with this program; if not, write to the Free Software
29    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30    02110-1301, USA.
31
32    Bacula® is a registered trademark of Kern Sibbald.
33    The licensor of Bacula is the Free Software Foundation Europe
34    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
35    Switzerland, email:ftf@fsfeurope.org.
36 */
37
38 /*
39   to create datafile
40  
41   for i in $(seq 10000 99999) ; do
42      j=$((($i % 1000) + 555))
43      echo "$i;/tmp/totabofds$j/fiddddle${j}$i;xxxLSTATxxxx;xxxxxxxMD5xxxxxx"
44   done  > dat1
45  
46   or
47
48   j=0
49   find / | while read a; do
50    j=$(($j+1))
51    echo "$j;$a;xxxLSTATxxxx;xxxxxxxMD5xxxxxx"
52   done > dat1
53  */
54
55 #include "bacula.h"
56 #include "stored/stored.h"
57 #include "findlib/find.h"
58 #include "cats/cats.h"
59  
60 /* Forward referenced functions */
61 static void *do_batch(void *);
62
63
64 /* Local variables */
65 static B_DB *db;
66
67 static const char *db_name = "bacula";
68 static const char *db_user = "bacula";
69 static const char *db_password = "";
70 static const char *db_host = NULL;
71
72 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
73
74 static void usage()
75 {
76    fprintf(stderr, _(
77 PROG_COPYRIGHT
78 "\nVersion: %s (%s)\n"
79 "Example : bbatch -w /path/to/workdir -h localhost -f dat1 -f dat -f datx\n"
80 " will start 3 thread and load dat1, dat and datx in your catalog\n"
81 "See bbatch.c to generate datafile\n\n"
82 "Usage: bbatch [ options ] -w working/dir -f datafile\n"
83 "       -d <nn>           set debug level to <nn>\n"
84 "       -dt               print timestamp in debug output\n"
85 "       -n <name>         specify the database name (default bacula)\n"
86 "       -u <user>         specify database user name (default bacula)\n"
87 "       -P <password      specify database password (default none)\n"
88 "       -h <host>         specify database host (default NULL)\n"
89 "       -w <working>      specify working directory\n"
90 "       -v                verbose\n"
91 "       -f <file>         specify data file\n"
92 "       -?                print this message\n\n"), 2001, VERSION, BDATE);
93    exit(1);
94 }
95
96 /* number of thread started */
97 int nb=0;
98
99 int main (int argc, char *argv[])
100 {
101    int ch;
102    setlocale(LC_ALL, "");
103    bindtextdomain("bacula", LOCALEDIR);
104    textdomain("bacula");
105    init_stack_dump();
106    lmgr_init_thread();
107    
108    char **files = (char **) malloc (10 * sizeof(char *));
109    int i;
110    my_name_is(argc, argv, "bbatch");
111    init_msg(NULL, NULL);
112
113    OSDependentInit();
114
115    while ((ch = getopt(argc, argv, "h:c:d:n:P:Su:vf:w:?")) != -1) {
116       switch (ch) {
117       case 'd':                    /* debug level */
118          if (*optarg == 't') {
119             dbg_timestamp = true;
120          } else {
121             debug_level = atoi(optarg);
122             if (debug_level <= 0) {
123                debug_level = 1;
124             }
125          }
126          break;
127
128       case 'h':
129          db_host = optarg;
130          break;
131
132       case 'n':
133          db_name = optarg;
134          break;
135
136       case 'w':
137          working_directory = optarg;
138          break;
139
140       case 'u':
141          db_user = optarg;
142          break;
143
144       case 'P':
145          db_password = optarg;
146          break;
147
148       case 'v':
149          verbose++;
150          break;
151
152       case 'f':
153          if (nb < 10 ) {
154             files[nb++] = optarg;
155          }
156          break;
157
158       case '?':
159       default:
160          usage();
161
162       }
163    }
164    argc -= optind;
165    argv += optind;
166
167    if (argc != 0) {
168       Pmsg0(0, _("Wrong number of arguments: \n"));
169       usage();
170    }
171
172 #ifdef HAVE_BATCH_FILE_INSERT
173    printf("With new Batch mode\n");
174 #else
175    printf("Without new Batch mode\n");
176 #endif
177    i = nb;
178    while (--i >= 0) {
179       pthread_t thid;
180       JCR *bjcr = new_jcr(sizeof(JCR), NULL);
181       bjcr->bsr = NULL;
182       bjcr->VolSessionId = 1;
183       bjcr->VolSessionTime = (uint32_t)time(NULL);
184       bjcr->NumReadVolumes = 0;
185       bjcr->NumWriteVolumes = 0;
186       bjcr->JobId = getpid();
187       bjcr->set_JobType(JT_CONSOLE);
188       bjcr->set_JobLevel(L_FULL);
189       bjcr->JobStatus = JS_Running;
190       bjcr->where = bstrdup(files[i]);
191       bjcr->job_name = get_pool_memory(PM_FNAME);
192       pm_strcpy(bjcr->job_name, "Dummy.Job.Name");
193       bjcr->client_name = get_pool_memory(PM_FNAME);
194       pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
195       bstrncpy(bjcr->Job, "bbatch", sizeof(bjcr->Job));
196       bjcr->fileset_name = get_pool_memory(PM_FNAME);
197       pm_strcpy(bjcr->fileset_name, "Dummy.fileset.name");
198       bjcr->fileset_md5 = get_pool_memory(PM_FNAME);
199       pm_strcpy(bjcr->fileset_md5, "Dummy.fileset.md5");
200       
201       if ((db=db_init_database(NULL, db_name, db_user, db_password,
202                                db_host, 0, NULL, 0)) == NULL) {
203          Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
204       }
205       if (!db_open_database(NULL, db)) {
206          Emsg0(M_ERROR_TERM, 0, db_strerror(db));
207       }
208       Dmsg0(200, "Database opened\n");
209       if (verbose) {
210          Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
211       }
212       
213       bjcr->db = db;
214
215       pthread_create(&thid, NULL, do_batch, bjcr);
216    }
217
218    while (nb > 0) {
219       bmicrosleep(1,0);
220    }
221
222    return 0;
223 }
224
225 static void fill_attr(ATTR_DBR *ar, char *data)
226 {
227    char *p;
228    char *b;
229    int index=0;
230    ar->Stream = STREAM_UNIX_ATTRIBUTES;
231    ar->JobId = getpid();
232
233    for(p = b = data; *p; p++) {
234       if (*p == ';') {
235          *p = '\0';
236          switch (index) {
237          case 0:
238             ar->FileIndex = str_to_int64(b);
239             break;
240          case 1:
241             ar->fname = b;
242             break;
243          case 2:
244             ar->attr = b;
245             break;
246          case 3:
247             ar->Digest = b;
248             break;
249          }
250          index++;
251          b = ++p;
252       }
253    }
254 }
255
256 static void *do_batch(void *jcr)
257 {
258    JCR *bjcr = (JCR *)jcr;
259    char data[1024];
260    int lineno = 0;
261    struct ATTR_DBR ar;
262    memset(&ar, 0, sizeof(ar));
263    btime_t begin = get_current_btime();
264    char *datafile = bjcr->where;
265
266    FILE *fd = fopen(datafile, "r");
267    if (!fd) {
268       Emsg1(M_ERROR_TERM, 0, _("Error opening datafile %s\n"), datafile);
269    }
270    while (fgets(data, sizeof(data)-1, fd)) {
271       strip_trailing_newline(data);
272       lineno++;
273       if (verbose && ((lineno % 5000) == 1)) {
274          printf("\r%i", lineno);
275       }
276       fill_attr(&ar, data);
277       if (!db_create_file_attributes_record(bjcr, bjcr->db, &ar)) {
278          Emsg0(M_ERROR_TERM, 0, _("Error while inserting file\n"));
279       }
280    }
281    fclose(fd);
282    db_write_batch_file_records(bjcr);
283    btime_t end = get_current_btime();
284    
285    P(mutex);
286    char ed1[200], ed2[200];
287    printf("\rbegin = %s, end = %s\n", edit_int64(begin, ed1),edit_int64(end, ed2));
288    printf("Insert time = %sms\n", edit_int64((end - begin) / 10000, ed1));
289    printf("Create %u files at %.2f/s\n", lineno, 
290           (lineno / ((float)((end - begin) / 1000000))));
291    nb--;
292    V(mutex);
293    pthread_exit(NULL);
294    return NULL;
295 }