~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to allpairs/src.tpds/allpairs_cleanup.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2005- The University of Notre Dame
 
3
This software is distributed under the GNU General Public License.
 
4
See the file COPYING for details.
 
5
*/
 
6
 
 
7
#include <stdio.h>
 
8
#include <stdlib.h>
 
9
#include <unistd.h>
 
10
#include <string.h>
 
11
 
 
12
#include "auth_all.h"
 
13
#include "debug.h"
 
14
#include "chirp_matrix.h"
 
15
 
 
16
void printUsage(char* cmd) {
 
17
        printf("Use: %s <options> -F [finalize file]",cmd);
 
18
        printf(" where options are:\n");
 
19
        printf(" -a <mode>      Explicit authentication mode.\n");
 
20
        printf(" -d <subsystem> Enable debugging for this subsystem.  (Try -d all to start.)\n");
 
21
        //printf(" -t <string>    Timeout, e.g. 60s\n");
 
22
        printf(" -D <file>      Download Results Matrix to a file.\n");
 
23
        printf(" -R             Remove remote state.\n");
 
24
        printf(" -L             Remove local state.\n");
 
25
        printf(" -M             Remove results matrix.\n");
 
26
        printf(" -h             Show this help screen\n");
 
27
}
 
28
 
 
29
int main(int argc, char** argv) {
 
30
    char cl;
 
31
    int did_explicit_auth = 0;
 
32
    int download,rm_local,rm_remote,rm_mat,file_provided;
 
33
    int rm_remote_error = 0;
 
34
    char matrix_target[CHIRP_PATH_MAX];
 
35
    char finalize_file[CHIRP_PATH_MAX];
 
36
    time_t stoptime = time(0)+3600;
 
37
    
 
38
    download=rm_local=rm_remote=rm_mat=file_provided=0;
 
39
 
 
40
    while((cl=getopt(argc,argv,"+a:d:hD:LRMF:"))!=(char)-1) {
 
41
        switch(cl) {
 
42
        case 'a':
 
43
            auth_register_byname(optarg);
 
44
            did_explicit_auth = 1;
 
45
            break;
 
46
        case 'd':
 
47
            debug_flags_set(optarg);
 
48
            break;
 
49
        case 'h':
 
50
                printUsage(argv[0]);
 
51
                exit(0);
 
52
                break;
 
53
        case 'D': // download matrix data to local disk
 
54
            download=1;
 
55
            strcpy(matrix_target,optarg);
 
56
            break;
 
57
        case 'L': // force LOCAL state removal
 
58
            rm_local=1;
 
59
            break;
 
60
        case 'R': // force REMOTE state removal (chirp_distribute -X)
 
61
            rm_remote=1;
 
62
            break;
 
63
        case 'M': // force REMOTE MATRIX state removal 
 
64
            rm_mat=1;
 
65
            break; 
 
66
        case 'F':
 
67
            file_provided=1;
 
68
            strcpy(finalize_file,optarg);
 
69
            break;
 
70
        }
 
71
    }
 
72
 
 
73
    if(!file_provided) {
 
74
        fprintf(stderr,"Please provide argument -F [finalize file]\n");
 
75
        printUsage(argv[0]);
 
76
        exit(1);
 
77
    }
 
78
 
 
79
     if(!did_explicit_auth) auth_register_all(); // if an authentication mechanism wasn't chosen, default register all.
 
80
     debug_config(argv[0]); // indicate what string to use as the executable name when printing debugging information
 
81
    // first, parse finalize file to get information.
 
82
    char* cmd;
 
83
    char* wID;
 
84
    char* local_dir;
 
85
    char* mat_host;
 
86
    char* mat_path;
 
87
    char* remote_dir;
 
88
    char* node_list;
 
89
    char* hn;
 
90
    char* fun_path;
 
91
    int strlentmp;
 
92
    FILE* fp = fopen(finalize_file,"r");
 
93
    if(!fp) {
 
94
        fprintf(stderr,"Finalize file not readable.\n");
 
95
        exit(1);
 
96
    }
 
97
 
 
98
    // 0th item is workload id
 
99
    if(fscanf(fp, " wID=%i ",&strlentmp) == 1) {
 
100
        wID = malloc((strlentmp+1)*sizeof(char));
 
101
        if(!wID)
 
102
        {
 
103
            fprintf(stderr,"Could not allocate %i bytes for workload ID\n",strlentmp);
 
104
            exit(1);
 
105
        }
 
106
        if(fscanf(fp, " %s ", wID) != 1) {
 
107
            fprintf(stderr,"Could not read in workload ID\n");
 
108
            exit(2);
 
109
        }
 
110
    }
 
111
    
 
112
    
 
113
    // first item is local prefix -- remove everything.
 
114
    if(fscanf(fp, " local_dir=%i ",&strlentmp) == 1) {
 
115
        local_dir = (char*) malloc((strlentmp+1)*sizeof(char));
 
116
        if(!local_dir)
 
117
        {
 
118
            fprintf(stderr,"Could not allocate %i bytes for local directory\n",strlentmp);
 
119
            exit(1);
 
120
        }
 
121
        if(fscanf(fp," %s ", local_dir) != 1) {
 
122
            fprintf(stderr,"Could not read in local directory\n");
 
123
            exit(2);
 
124
        }
 
125
    }
 
126
 
 
127
    // second item is matrix host -\ remove
 
128
    // third item is matrix path  -/ matrix
 
129
    if(fscanf(fp, " mat_host=%i ",&strlentmp) == 1) {
 
130
        mat_host = (char *) malloc((strlentmp+1)*sizeof(char));
 
131
        if(!mat_host)
 
132
        {
 
133
            fprintf(stderr,"Could not allocate %i bytes for matrix host\n",strlentmp);
 
134
            exit(1);
 
135
        }
 
136
        if(fscanf(fp," %s ", mat_host) != 1) {
 
137
            fprintf(stderr,"Could not read in matrix host\n");
 
138
            exit(2);
 
139
        }
 
140
    }
 
141
 
 
142
    if(fscanf(fp, " mat_path=%i ",&strlentmp) == 1) {
 
143
        mat_path = (char *) malloc((strlentmp+1)*sizeof(char));
 
144
        if(!mat_path)
 
145
        {
 
146
            fprintf(stderr,"Could not allocate %i bytes for matrix path\n",strlentmp);
 
147
            exit(1);
 
148
        }
 
149
        if(fscanf(fp," %s ", mat_path) != 1) {
 
150
            fprintf(stderr,"Could not read in matrix path\n");
 
151
            exit(2);
 
152
        }
 
153
    }
 
154
    
 
155
 
 
156
    // 4th item is chirp_dirname
 
157
    if(fscanf(fp, " remote_dir=%i ",&strlentmp) == 1) {
 
158
        remote_dir = (char *) malloc((strlentmp+1)*sizeof(char));
 
159
        if(!remote_dir)
 
160
        {
 
161
            fprintf(stderr,"Could not allocate %i bytes for remote path\n",strlentmp);
 
162
            exit(1);
 
163
        }
 
164
        if(fscanf(fp," %s ", remote_dir) != 1) {
 
165
            fprintf(stderr,"Could not read in remote path\n");
 
166
            exit(2);
 
167
        }
 
168
    }
 
169
    if(rm_remote==1) {
 
170
        fprintf(stderr,"Asked to remove remote state, but there is no remote state specified.\n");
 
171
        rm_remote_error = 1;
 
172
    }
 
173
     
 
174
    // 7th item is full goodstring
 
175
    if(fscanf(fp, " node_list=%i ",&strlentmp) == 1) {
 
176
        node_list = (char *) malloc((strlentmp+1)*sizeof(char));
 
177
        if(!node_list)
 
178
        {
 
179
            fprintf(stderr,"Could not allocate %i bytes for remote hosts\n",strlentmp);
 
180
            exit(1);
 
181
        }
 
182
        if(fread(node_list,1,strlentmp,fp) != strlentmp) {
 
183
            fprintf(stderr,"Could not read in remote hosts\n");
 
184
            exit(2);
 
185
        }
 
186
    }
 
187
    if(rm_remote==1 && rm_remote_error == 0) {
 
188
        fprintf(stderr,"Asked to remove remote state, but there is no remote state specified.\n");
 
189
        rm_remote_error = 1;
 
190
    }
 
191
 
 
192
    // 9th item is hostname
 
193
    if(fscanf(fp, " host=%i ",&strlentmp) == 1) {
 
194
        hn = malloc((strlentmp+1)*sizeof(char));
 
195
        if(!hn)
 
196
        {
 
197
            fprintf(stderr,"Could not allocate %i bytes for hostname\n",strlentmp);
 
198
            exit(1);
 
199
        }
 
200
        if(fscanf(fp, " %s ", hn) != 1) {
 
201
            fprintf(stderr,"Could not read in hostname\n");
 
202
            exit(2);
 
203
        }
 
204
    }
 
205
    if(rm_remote==1 && rm_remote_error == 0) {
 
206
        fprintf(stderr,"Asked to remove remote state, but there is no remote state specified.\n");
 
207
        rm_remote_error = 1;
 
208
    }
 
209
    
 
210
    // 10th item is full function directory -- remove tarball, exception list
 
211
    if(fscanf(fp, " fun_path=%i ",&strlentmp) == 1) {
 
212
        fun_path = (char *) malloc((strlentmp+1)*sizeof(char));
 
213
        if(!fun_path)
 
214
        {
 
215
            fprintf(stderr,"Could not allocate %i bytes for function directory\n",strlentmp);
 
216
            exit(1);
 
217
        }
 
218
        if(fscanf(fp," %s ", fun_path) != 1) {
 
219
            fprintf(stderr,"Could not read in function directory\n");
 
220
            exit(2);
 
221
        }
 
222
        cmd = (char *) malloc((strlen(fun_path)+strlen("rm -f ")+strlen(wID)+strlen(".func.tar")+12)*sizeof(char));
 
223
        if(!cmd)
 
224
        {
 
225
            fprintf(stderr,"Could not allocate memory for command\n");
 
226
            exit(1);
 
227
        }
 
228
        sprintf(cmd,"rm -f %s/%s.func.tar",fun_path,wID);
 
229
        if(system(cmd)) { fprintf(stderr,"Could not remove %s/%s.func.tar\n",fun_path,wID); exit(1);}
 
230
        sprintf(cmd,"rm -f %s/exclude.list",fun_path);
 
231
        if(system(cmd)) { fprintf(stderr,"Could not remove %s/exclude.list\n",fun_path); exit(1);}
 
232
        free(cmd);
 
233
        cmd=NULL;
 
234
        
 
235
    }
 
236
    else {
 
237
        // internal function
 
238
        fun_path = NULL;
 
239
    }
 
240
    
 
241
    
 
242
    // end parsing finalize file
 
243
    
 
244
    // next, download if desired.
 
245
   
 
246
    if(download) {
 
247
        fprintf(stderr,"Download Matrix Mode\n");
 
248
        FILE* mt = fopen(matrix_target, "w");
 
249
        struct chirp_matrix* m =  chirp_matrix_open( mat_host, mat_path, stoptime);
 
250
        if(m) {
 
251
        int w = chirp_matrix_width( m );
 
252
        int h = chirp_matrix_height( m );
 
253
        //int e = chirp_matrix_element_size( m );
 
254
        double* buf = malloc(w*sizeof(double));
 
255
        int x,y;
 
256
        for(y=0; y < h; y++) {
 
257
            chirp_matrix_get_row( m , y, buf, stoptime );
 
258
            for(x=0; x<w; x++) {
 
259
                fprintf(mt,"%i %i %.2lf\n",y,x,buf[x]);
 
260
            }
 
261
        }
 
262
        }
 
263
        else
 
264
        {
 
265
            printf("Could not open matrix %s %s\n",mat_host,mat_path);
 
266
            return 1;
 
267
        }
 
268
    }
 
269
    // next, delete remote state if desired.
 
270
    if(rm_remote && !rm_remote_error) {
 
271
        fprintf(stderr,"Remove Remote State Mode\n");
 
272
        cmd = (char *) malloc(strlen("chirp_distribute -a hostname -X ")+10+(2*strlen(hn))+1+strlen(remote_dir)+1+strlen(node_list)+1);
 
273
        if(cmd == NULL) {
 
274
            fprintf(stderr,"Allocating distribute command string memory failed!\n");
 
275
            return 1;
 
276
        }
 
277
        sprintf(cmd,"chirp_distribute -a hostname -X %s %s %s",hn,remote_dir,node_list);
 
278
        debug(D_CHIRP,"%s\n",cmd);
 
279
        system(cmd);
 
280
        sprintf(cmd,"chirp_distribute -a hostname -X %s %s %s",hn,remote_dir,hn);
 
281
        debug(D_CHIRP,"%s\n",cmd);
 
282
        system(cmd);
 
283
        free(cmd);
 
284
        cmd=NULL;
 
285
    }
 
286
    // next, delete matrix if desired.
 
287
     if(rm_mat) {
 
288
        fprintf(stderr,"Remove Matrix State Mode\n");
 
289
        chirp_matrix_delete( mat_host, mat_path, time(0)+600 );
 
290
        
 
291
    }
 
292
    // next, delete local if desired.
 
293
     if(rm_local) {
 
294
         fprintf(stderr,"Remove Local State Mode\n");
 
295
         cmd = (char *) malloc(strlen("rm -rf ")+1+strlen(local_dir)+1);
 
296
         if(cmd == NULL) {
 
297
             fprintf(stderr,"Allocating distribute command string memory failed!\n");
 
298
             return 1;
 
299
         }
 
300
         sprintf(cmd,"rm -rf %s",local_dir);
 
301
         system(cmd);
 
302
         free(cmd);
 
303
         cmd=NULL;
 
304
     }
 
305
 
 
306
     return 0;
 
307
    
 
308
}