Esempio n. 1
0
/* kill all processes other than the given pid that have the given processName */
void killOtherProcessesWithName(pid_t pid, char* processName) {
        /* get the user running the parent process */
        char* userName = calloc(MAX_USERNAME_LENGTH + 1, sizeof(char));
        getUserNameForPid(pid, userName);

        /* build the command */
        char* command = calloc(55 + strlen(processName) + strlen(userName),
                        sizeof(char));
        sprintf(command,
                        "ps aux | grep %s | grep -v gdb | grep -v grep | grep %s",
                        processName, userName);

        /* get the number of processes */
        int numPids = countLines(command);

        /* get the pids */
        int pids[numPids];
        getPids(command, pids);

        /* for each process pid, if it's not yours, kill it */
        int index;
        for (index = 0; index < numPids; index++) {
                int otherpid = pids[index];
                if (otherpid != pid) {
                        /* printf("Killing procnanny  process found with pid %d\n",otherpid); */
                        if (kill(otherpid, 9) == -1) {
                                printMessageandExit("Kill error\n", EXIT_FAILURE);
                        }
                }
        }

        // clean up
        free(command);
        free(userName);
}
int
main( int argc, char *argv[] )
{
    char* queryFile = NULL;
    char* mount = NULL; 
    char* jobId = NULL;
    char offsetMPI[4096]; 
    char timeMPI[4096];
    int c;
    int ret = 0;
    /* with the -p flag, this is set to true and the processor graphs
     * will be generated */
    bool processorGraph = false; 
    char* outputFile = NULL;
    int numBins = 500; 
    int numIndexFiles, size; 
    double binSize; 
    double minMax[2]; /* space for the min and max time */
    double endSum, stdev;
    /* get cutoffs for processor graphs: default is one above */
    bool above = false; 
    bool below = false; 
    int numStdDev = 1;
    while ((c = getopt(argc, argv, "m:q:n:o:pabs:j:")) != -1) {
        switch(c)
            {
            case 'm':
                mount = optarg;
                break;
            case 'q':
                queryFile = optarg; 
                break; 
            case 'o':
                outputFile = optarg;
                break;
            case 'n':
                numBins = atoi(optarg); 
                break;
            case 'p':
                processorGraph = true; 
                break;
            case 'j':
                jobId = optarg; 
                break;
            case 'a':
                above = true; 
                break; 
            case 'b':
                below = true; 
                break; 
            case 's':
                numStdDev = atoi(optarg); 
                break; 
            case '?':
                printf("Unknown option %c\n", optopt); 
                usage();
                return -1; 
            }
    }
    /* if both below and above are false, make default where above = true */
    if (below == false && above == false) 
    {
        above = true; 
    }
    if (processorGraph)
    {
        strcpy(offsetMPI, outputFile); 
        strcat(offsetMPI, "offsets");
        strcat(offsetMPI, jobId); 
        strcpy(timeMPI, outputFile); 
        strcat(timeMPI, "times"); 
        strcat(timeMPI, jobId); 
    }
    size = init(argc, argv); 
    int rank; 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    numIndexFiles = getNumberOfIndexFiles(queryFile); 
    int* pids = (int *)calloc(numIndexFiles, sizeof(int)); 
    if (pids == NULL) {
        printf("Could not allocate memory for pids\n"); 
        return -1; 
    }
    if (rank == 0) {
        getPids(pids, queryFile); 
        if (numIndexFiles == 0) 
        {
            printf("ERROR:Number of Index Files found was %d\n", numIndexFiles);
            return -1; 
        }
    }
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Bcast(pids, numIndexFiles, MPI_INT, 0, MPI_COMM_WORLD); 
    /* there was a problem so exit */
    if (size == -1) 
    {
        printf("ERROR:The size of the mpi comm world was -1\n"); 
        MPI_Finalize();
        return -1; 
    }
    double* endTimes = (double *)calloc(numIndexFiles, sizeof(double)); 
    if (endTimes == NULL) 
    {
        printf("ERROR:Could not allocate memory for ending Times\n"); 
        return -1; 
    }
    ret = getMaxMinTimes(numIndexFiles, size, mount, minMax, &endSum, endTimes,pids); 
    if (ret != 0)
        MPI_Abort(MPI_COMM_WORLD, ret);
    double average; 
    if (rank == 0) {
        average = endSum/numIndexFiles;
    }
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Bcast(&average, 1, MPI_LONG_DOUBLE, 0, MPI_COMM_WORLD); 
    if (rank == 0) {
        binSize = (minMax[1] - minMax[0])/numBins;
    }
    MPI_Barrier(MPI_COMM_WORLD); 
    MPI_Bcast(&binSize, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); 
    /* allocate data to place final counts in  */
    double * bandwidths = (double *)calloc(numBins, sizeof(double)); 
    long long * iosTime = (long long *)calloc(numBins, sizeof(long long)); 
    long long * iosFin = (long long *)calloc(numBins, sizeof(long long)); 
    /* we need 51 bins here because there are 10 bins between each 
    * size and the last is for those above PiB */
    long long * writeCount = (long long *)calloc(51, sizeof(long long)); 
    if ((bandwidths == NULL) | (iosTime == NULL) | (iosFin == NULL) 
        | (writeCount == NULL)) 
    {
        printf("Could not allocate data placements\n"); 
        MPI_Abort(MPI_COMM_WORLD, -1);
    }
    ret = parseData(numIndexFiles, size, mount, binSize, numBins, 
                bandwidths, iosTime, iosFin, writeCount, minMax[0], 
                average, &stdev, pids); 
    if (ret != 0)
        MPI_Abort(MPI_COMM_WORLD, ret);
    MPI_Barrier(MPI_COMM_WORLD); 
    MPI_Bcast(&stdev, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
    int retv; 
    if (processorGraph) 
    {
        ret = writeProcessorData(numIndexFiles, size, mount, offsetMPI,average, stdev,
                            endTimes, timeMPI, minMax[0], above, below, 
                            numStdDev, pids);
        if (ret != 0)
            MPI_Abort(MPI_COMM_WORLD, ret);
    }
    int numAbove = 0; 
    int numBelow = 0; 
    MPI_Barrier(MPI_COMM_WORLD); 
    if (rank == 0) {
        strcat(outputFile, jobId); 
        strcat(outputFile, "output.txt"); 
        for (int i = 0; i < numIndexFiles; i++) {
            /* what is graphed and where in relation to the average line
             * it is for the processor graph*/
            if ((above && (endTimes[i] > (average+(numStdDev*stdev)))) ||
                ((numIndexFiles <= 16) && (endTimes[i] > average))) {
                numAbove++; 
            }
            else if ((below && (endTimes[i] < (average-(numStdDev*stdev)))) ||
                ((numIndexFiles <= 16) && (endTimes[i] < average))){
                numBelow++; 
            }
        }
        retv = writeOutputText(outputFile,numBins,minMax,binSize,bandwidths,
                                iosTime,iosFin,writeCount, average, numAbove, 
                                numBelow);
        if (retv != 0) {
            printf("Writing output file failed!\n");
            return retv; 
        }
    }
    free(pids); 
    free(endTimes); 
    free(bandwidths); 
    free(iosTime); 
    free(iosFin); 
    free(writeCount);
    MPI_Finalize(); 
    return 0; 
}