#include <sys/stat.h>
#include <string.h>
#include <assert.h>
+#include <errno.h>
#include "shmtrans.h"
#include "worker.h"
-#define BUFFSIZE = 10000
+#define BUFLEN 200
+#define NUMDISKS 22
+#define NUMEBS 16
+
+
FILE *popen(const char *, const char *);
char *strdup(const char *);
-const char data[22][8] = {"/data01", "/data02", "/data03", "/data04", "/data05",
+const char data[NUMDISKS][8] = {"/data01", "/data02", "/data03", "/data04", "/data05",
"/data06", "/data07", "/data08", "/data09", "/data10",
"/data11", "/data12", "/data13", "/data14", "/data15",
"/data16", "/data17", "/data18", "/data19", "/data20",
char diskNum[] = "diskNum";
-int data_disks[22]; /* Available disk space */
+int data_disks[NUMDISKS]; /* Available disk space */
char shmemBase[] = "daq_evtbuild"; /* Base name for shared memory segments */
-int shmem_nums[16]; /* Numbers for found shared memory segments */
+int shmem_nums[NUMEBS]; /* Numbers for found shared memory segments */
int nrOfShmSegms; /* Total number of found shared memory segments */
typedef struct TheArgsS {
- int excludeDisks[22];
+ int excludeDisks[NUMDISKS];
int excludeDisksCnt;
int sleepTime; /*seconds*/
+ int testPartition;
int debug;
} TheArgs;
static void usage(const char *progName)
{
- printf( "Usage: %s\n", progName);
- printf( " [-h|--help] : Print this help.\n");
- printf( " [-e|--exclude <diskNr>] : Nr of disk to exclude.\n");
- printf( " [-s|--sleep <time>] : Sleep time.\n");
- printf( " [-D|--debug] : Debug output.\n");
+ printf( "daq_disks V 1.1 14-Feb-2012 \n");
+ printf( " authors: Sergei Yurevich\n");
+ printf( " Joern Adamczewski-Musch (j.adamczewski@gsi.de)\n");
+ printf( "Eventbuilder disk load balancer for HADES\n");
+ printf( "Usage: %s\n", progName);
+ printf( " [-h|--help] : Print this help.\n");
+ printf( " [-e|--exclude <diskNr>] : Nr of disk to exclude.\n");
+ printf( " [-a|--autoexclude] : Automatically exclude broken partition.\n");
+ printf( " [-s|--sleep <time>] : Sleep time.\n");
+ printf( " [-D|--debug] : Debug output.\n");
}
static void argsDefault(TheArgs *my)
{
my->sleepTime = 10;
my->debug = 0;
+ my->testPartition = 0;
my->excludeDisksCnt = 0;
/* Initialize the array with shared mem segment numbers */
int i;
- for(i=0; i<16; i++){
+ for(i=0; i<NUMEBS; i++){
shmem_nums[i] = -1;
}
}
{"help", 0, 0, 'h'},
{"sleep", 1, 0, 's'},
{"exclude", 1, 0, 'e'},
+ {"autoexclude", 0, 0, 'a'},
{"debug", 0, 0, 'D'},
{0, 0, 0, 0}
};
- i = getopt_long(argc, argv, "Dhs:e:", long_options,
+ i = getopt_long(argc, argv, "Dhas:e:", long_options,
&option_index);
if (i == -1)
break;
case 'e':
my->excludeDisks[my->excludeDisksCnt] = atoi(optarg);
my->excludeDisksCnt++;
- break;
+ case 'a':
+ my->testPartition=1;
+ break;
+
case 'D':
my->debug = 1;
break;
+
default:
usage(argv[0]);
return -1;
return retval;
}
+
+static int testPartition(TheArgs *theArgs, int diskIndex)
+{
+ /* return 1 if partition is writable, 0 otherwise*/
+ int err;
+ char fname[BUFLEN] = "";
+
+ if((diskIndex<0) || (diskIndex>=NUMDISKS)) return 0;
+ if(theArgs->testPartition==0) return 1;
+ snprintf(fname,BUFLEN,"%s/data/daqDiskCheck",data[diskIndex]);
+ if(theArgs->debug)
+ printf("Debug: testPartition %d with file %s\n", diskIndex, fname);
+ FILE* tfile = fopen(fname, "w+");
+ if(tfile==NULL) {
+ err=errno; /* save errno not to be modified by printf...*/
+ printf("Error: testPartition for %s failed with errno %d - %s\n",data[diskIndex], errno, strerror(err));
+ return 0;
+ }
+ if(theArgs->debug)
+ printf("Debug: testPartition for %s OK\n",data[diskIndex]);
+ fclose(tfile);
+ return 1;
+}
+
+
static void initDataDisks(int val)
{
int i;
- for(i=0; i<22; i++){
+ for(i=0; i<NUMDISKS; i++){
data_disks[i] = val;
}
}
/* Read the available disk space with 'df' */
int count;
- char buff[200] = "";
+ char buff[BUFLEN] = "";
FILE* stream = popen("/bin/df -m", "r");
char **array;
- array = malloc( sizeof(char *) * 100 );
+ array = malloc( sizeof(char *) * BUFLEN );
assert(array != NULL);
count = 0;
while (1)
{
- fgets( buff, 200, stream );
+ fgets( buff, BUFLEN, stream );
if( feof(stream) )
break;
for( ; *array != NULL; array++ ){
int k;
- for(k=0; k<22; k++){
+ for(k=0; k<NUMDISKS; k++){
/* Exclude disks */
if(excludeDisks(theArgs, k+1))
if(ptr == NULL)
continue;
+ /* New: test if partition is writable*/
+ if(!testPartition(theArgs, k))
+ continue;
+
/* Split string by spaces */
strcpy(tmp,*array);
token = strtok(tmp, space); /* token => "Filesystem" */
if(theArgs->debug){
int i;
- for(i=0; i<22; i++){
+ for(i=0; i<NUMDISKS; i++){
printf("Debug: sizes[%2d] = %d\n", i, data_disks[i]);
}
}
int maxElement = 0;
int maxIndex = 0;
- for( k=0; k<22; k++ ){
+ for( k=0; k<NUMDISKS; k++ ){
if( tmp[k] > maxElement ){
maxElement = tmp[k];
maxIndex = k;
int i;
char shmemName[20];
- int tmp_data_disks[22];
+ int tmp_data_disks[NUMDISKS];
int diskSpace;
int diskIndex;
unsigned long int iMax;
unsigned long int freeDiskNr;
/* Temp copy of array with free disk space */
- for(i=0; i<22; i++){
+ for(i=0; i<NUMDISKS; i++){
tmp_data_disks[i] = data_disks[i];
}
- for(i=0; i<16; i++){
+ for(i=0; i<NUMEBS; i++){
/* The array contains endings (EB numbers) of shared memory names
* which contain diskNum variable.
static int findShmem(TheArgs *theArgs)
{
/*
- * Loop over all EB numbers (1..16)
+ * Loop over all EB numbers (1..NUMEBS)
* and find shared memory segments opened by daq_evtbuild.
* Save them in a shmem_nums array.
*/
- char shmemName[100];
- char shmemPathName[100];
+ char shmemName[BUFLEN];
+ char shmemPathName[BUFLEN];
int i;
int j = 0;
unsigned long int tmp;
- for(i=0; i<17; i++){
+ for(i=0; i<=NUMEBS; i++){
sprintf( shmemName, "%s%d", shmemBase, i );
sprintf( shmemPathName, "/dev/shm/%s%d.shm", shmemBase, i );