コード例 #1
0
  int readCpuCounters(SFLHost_cpu_counters *cpu) {
    int gotData = NO;

    kstat_ctl_t *kc;
    kstat_t *ksp = NULL;
    kstat_named_t *knp;

    kc = kstat_open();
    if (NULL == kc) {
      myLog(LOG_ERR, "readCpuCounters kstat_open() failed");
    } else {
      ksp = kstat_lookup(kc, "unix", 0, "system_misc");
      if (NULL == ksp) {
	myLog(LOG_ERR, "kstat_loockup error (unix:*:system_misc:*)");
      }
    }

    if (NULL != ksp) {
      if (-1 == kstat_read(kc, ksp, NULL)) {
	myLog(LOG_ERR, "kstat_read error (module: %s, name: %s, class: %s)",
	      ksp->ks_module, ksp->ks_name, ksp->ks_class);
      } else {
	// load_one
	knp = kstat_data_lookup(ksp, "avenrun_1min");
	cpu->load_one = (float)knp->value.ui32;

	// load_five
	knp = kstat_data_lookup(ksp, "avenrun_5min");
	cpu->load_five = (float)knp->value.ui32;

	// load_fifteen
	knp = kstat_data_lookup(ksp, "avenrun_15min");
	cpu->load_fifteen = (float)knp->value.ui32;

	// proc_total
	knp = kstat_data_lookup(ksp, "nproc");
	cpu->proc_total = knp->value.ui32;

	// cpu_num
	knp = kstat_data_lookup(ksp, "ncpus");
	cpu->cpu_num = knp->value.ui32;

	// uptime
	knp = kstat_data_lookup(ksp, "boot_time");
	time_t boot = knp->value.ui32;
	time_t uptime = time(NULL) - boot;
	cpu->uptime = uptime;

	gotData = YES;
      }
    }

    ksp = kstat_lookup(kc, "cpu_info", -1, NULL);
    if (NULL == ksp) {
      myLog(LOG_ERR, "kstat_loockup error (cpu_info:*:cpu_info0:*)");
    }

    if (NULL != ksp) {
      if (-1 == kstat_read(kc, ksp, NULL)) {
	myLog(LOG_ERR, "kstat_read error (module: %s, name: %s, class: %s)",
	      ksp->ks_module, ksp->ks_name, ksp->ks_class);
      } else {

	// cpu_speed
	knp = kstat_data_lookup(ksp, "clock_MHz");
	cpu->cpu_speed = (uint32_t)knp->value.i32;

	gotData = YES;
      }
    }

    // running processes
    int running = runningProcesses();
    if(running > 0) {
      cpu->proc_run = running;
      gotData = YES;
    }

    // From Ganglia's libmetrics
#define CPUSTATES	5
#define CPUSTATE_IDLE	0
#define CPUSTATE_USER	1
#define CPUSTATE_KERNEL	2
#define CPUSTATE_IOWAIT	3
#define CPUSTATE_SWAP	4

    cpu_stat_t cpu_stat;
    int cpu_id = sysconf(_SC_NPROCESSORS_ONLN);
    uint64_t cpu_info[CPUSTATES] = { 0 };
    long stathz = sysconf(_SC_CLK_TCK);
    uint64_t interrupts = 0;
    uint64_t contexts = 0;
#ifndef KSNAME_BUFFER_SIZE
#define KSNAME_BUFFER_SIZE 32
#endif

#define STATHZ_TO_MS(t) (((t) * 1000) / stathz)

    char ks_name[KSNAME_BUFFER_SIZE];
    int i, n;
    for (i = 0; cpu_id > 0; i++) {
      n = p_online(i, P_STATUS);
      if (1 == n || (-1 == n && EINVAL == errno)) {
	continue;
      }

      snprintf(ks_name, KSNAME_BUFFER_SIZE, "cpu_stat%d", i);
      cpu_id--;

      ksp = kstat_lookup(kc, "cpu_stat", i, ks_name);
      if (NULL == ksp) {
	myLog(LOG_ERR, "kstat_lookup error (module: cpu_stat, inst: %d, name %s)", i, ks_name);
	continue;
      }

      if (-1 == kstat_read(kc, ksp, &cpu_stat)) {
	myLog(LOG_ERR, "kstat_read error (module: %s, name: %s, class: %s)",
	      ksp->ks_module, ksp->ks_name, ksp->ks_class);
	continue;
      }

      if(debug>1) {
	myLog(LOG_INFO, "adding cpu stats for cpu=%d (idle=%u user=%u wait=%u swap=%u kernel=%u)",
	      cpu_id,
	      cpu_stat.cpu_sysinfo.cpu[CPU_IDLE],
	      cpu_stat.cpu_sysinfo.cpu[CPU_USER],
	      cpu_stat.cpu_sysinfo.wait[W_IO] + cpu_stat.cpu_sysinfo.wait[W_PIO],
	      cpu_stat.cpu_sysinfo.wait[W_SWAP],
	      cpu_stat.cpu_sysinfo.cpu[CPU_KERNEL]);
      }
	      

      cpu_info[CPUSTATE_IDLE] += cpu_stat.cpu_sysinfo.cpu[CPU_IDLE];
      cpu_info[CPUSTATE_USER] += cpu_stat.cpu_sysinfo.cpu[CPU_USER];
      cpu_info[CPUSTATE_IOWAIT] += cpu_stat.cpu_sysinfo.wait[W_IO] + cpu_stat.cpu_sysinfo.wait[W_PIO];
      cpu_info[CPUSTATE_SWAP] += cpu_stat.cpu_sysinfo.wait[W_SWAP];
      cpu_info[CPUSTATE_KERNEL] += cpu_stat.cpu_sysinfo.cpu[CPU_KERNEL];
      interrupts += cpu_stat.cpu_sysinfo.intr + cpu_stat.cpu_sysinfo.trap;
      contexts += cpu_stat.cpu_sysinfo.pswitch;
      gotData = YES;
    }

    // cpu_user
    cpu->cpu_user = (uint32_t)STATHZ_TO_MS(cpu_info[CPUSTATE_USER]);

    // cpu_nice
    SFL_UNDEF_COUNTER(cpu->cpu_nice);

    // cpu_system
    cpu->cpu_system = (uint32_t)STATHZ_TO_MS(cpu_info[CPUSTATE_KERNEL]);

    // cpu_idle
    cpu->cpu_idle = (uint32_t)STATHZ_TO_MS(cpu_info[CPUSTATE_IDLE]);

    // cpu_wio
    cpu->cpu_wio = (uint32_t)STATHZ_TO_MS(cpu_info[CPUSTATE_IOWAIT]);

    // cpu_intr
    SFL_UNDEF_COUNTER(cpu->cpu_intr);

    // cpu_sintr
    SFL_UNDEF_COUNTER(cpu->cpu_sintr);
	
    // interrupts
    cpu->interrupts = interrupts;

    // contexts
    cpu->contexts = contexts;

    if(kc) kstat_close(kc);
    return gotData;
  }
コード例 #2
0
int main() {
    clock_t ticks;
    time_t start_time, end_time;
    time(&start_time);
    int i;
    int status = 0;
    log_file = fopen("log_file.txt", "w+");
    if (log_file == NULL) {
        fprintf(stderr, "LOG FILE CANNOT BE OPEN\n");
    }
    // initialize cpus
    for (i = 0; i < NUMBER_OF_PROCESSORS; i++) {
        CPU[i] = NULL;
    }
    init_();
    initializeProcessQueue(&readyQueue);
    initializeProcessQueue(&waitingQueue);

    // read in process and initialize process values
    while ((status = (readProcess(&processes[number_of_processes])))) {
        if (status == 1) {
            number_of_processes++;
        }
        if (number_of_processes > MAX_PROCESSES || number_of_processes == 0) {
            break;
        }
    }
    //sort process by their arrival times
    qsort(processes, number_of_processes, sizeof (process), compareByArrival);
    // main execution loop
    while (TRUE) {
        ticks = clock();
        incoming_process_init();
        running_process_to_waiting();
        most_ready_running_in_cpu();
        waiting_to_ready();

        refresh_processes();

        cpu_utilized_time += runningProcesses();
		simulation_time++;
        // break when there are no more running or incoming processes, and the waiting queue is empty
        if (runningProcesses() == 0 && (number_of_processes - nextProcess) == 0 && waitingQueue.size == 0) {
			break;
        }
    }
    // calculations based on CPU simulations
    int total_waiting_time = 0;
    int turn_around_time = 0;
    for (i = 0; i < number_of_processes; i++) {
        turn_around_time += processes[i].endTime - processes[i].arrivalTime;
        total_waiting_time += processes[i].waitingTime;
    }
    printf("********************************************************************\n");
    printf("Average Waiting Time\t\t\t:%.2f\n", total_waiting_time / (double) number_of_processes);
    printf("Average Turn Around Time\t\t:%.2f\n", turn_around_time / (double) number_of_processes);
    printf("Time all for all CPU processes\t\t:%d\n", simulation_time);
    printf("CPU Utilization Time\t\t\t:%.2f%c\n", (double) (cpu_utilized_time * 100.0) / (double) (simulation_time), (int) 37);
    printf("Total Number of Context Switches\t:%d\n", context_switches);
    printf("Last Process to finish ");
    for (i = 0; i < number_of_processes; i++) {
        if (processes[i].endTime == last_process_end_time) {
            printf("PID\t\t:%d\n", processes[i].pid);
        }
    }
	//printf("%d\n" , processes[number_of_processes].pid);
    printf("********************************************************************\n");
    time(&end_time);
    double prg_time = (end_time - start_time) * 0.001;
    double cpu_time = (double) ticks / CLOCKS_PER_SEC;
    fprintf(log_file, "Program Time\t:%.2fsecs\n", prg_time);
    fprintf(log_file, "CPU Time\t:%.2fsecs\n", cpu_time);
    fclose(log_file);
    return 0;
}
コード例 #3
0
int main(int argc, char **argv) {
    if (argc < 3) {
        fprintf(stderr, "Please enter a time quantums . . . . \n");
        exit(1);
    }
    time_slice = atoi(argv[1]);
    time_slice_1 = atoi(argv[2]);
    if (time_slice <= 0 || time_slice_1 <= 0) {
        fprintf(stderr, "Error! program usage rr < positive time quantum> < data file . . .\n");
        exit(1);
    }
    init_();
    clock_t ticks;
    time_t start_time, end_time;
    time(&start_time);
    int i;
    int status = 0;
    log_file = fopen("log_file.txt", "w+");
    if (log_file == NULL) {
        fprintf(stderr, "LOG FILE CANNOT BE OPEN\n");
    }
    //initialize cpu's
    for (i = 0; i < NUMBER_OF_PROCESSORS; i++) {
        CPU[i] = NULL;
    }
    initializeProcessQueue(&readyQueue);
    initializeProcessQueue(&waitingQueue);
    initializeProcessQueue(&level_one);
    initializeProcessQueue(&second_level);
    //initializeProcessQueue(&promoted);
    // read in process and initialize process values
    while ((status = (readProcess(&processes[number_of_processes])))) {
        if (status == 1) {
            number_of_processes++;
        }
    }
    if (number_of_processes > MAX_PROCESSES) {
        return -2;
    }
    if (number_of_processes == 0) {
        return -1;
    }
    int remaining_process = 0;
    //sort process by their arrival times
    qsort(processes, number_of_processes, sizeof (process), compareByArrival);
    // main execution loop
    while (TRUE) {
        ticks = clock();
        waiting_to_ready();
        incoming_process_init();
        running_process_to_waiting();
        most_ready_running_in_cpu();

        refresh_processes();
        increase_io_work();
        increase_cpu_work();

        cpu_utilized_time += runningProcesses();
        remaining_process = ex();
        // break when there are no more running or incoming processes, and the waiting queue is empty
        if (remaining_process == 0 && runningProcesses() == 0 && waitingQueue.size == 0) {
            break;
        }
        simulation_time++;
    }
    int total_waiting_time = 0;
    int turn_around_time = 0;
    for (i = 0; i < number_of_processes; i++) {
        turn_around_time += processes[i].endTime - processes[i].arrivalTime;
        total_waiting_time += processes[i].waitingTime;
    }
    printf(">>>>>>>>>>>>> FBQ with Q1 :%d\tQ2 :%d  <<<<<<<<<<<<<<<\n", time_slice, time_slice_1);
    printf("********************************************************************\n");
    printf("Average Waiting Time\t\t\t:%.2f\n", total_waiting_time / (double) number_of_processes);
    printf("Average Turn Around Time\t\t:%.2f\n", turn_around_time / (double) number_of_processes);
    printf("Time all for all CPU processes\t\t:%d\n", simulation_time);
    printf("CPU Utilization Time\t\t\t:%.2f%c\n", (double) (cpu_utilized_time * 100.0) / (double) (simulation_time), (int) 37);
    printf("Total Number of Context Switches\t:%d\n", context_switches);
    printf("Last Process to finish ");
    for (i = 0; i < number_of_processes; i++) {
        if (processes[i].endTime == simulation_time) {
            printf("PID\t\t:%d\n", processes[i].pid);
        }
    }
    printf("********************************************************************\n");
    time(&end_time);
    double prg_time = (end_time - start_time) * 0.001;
    double cpu_time = (double) ticks / CLOCKS_PER_SEC;
    fprintf(log_file, "Program Time\t:%.2fsecs\n", prg_time);
    fprintf(log_file, "CPU Time\t:%.2fsecs\n", cpu_time);
    fclose(log_file);
    return 0;
}
コード例 #4
0
int main(void) {
    int sumOfTurnaroundTimes = 0;
    int doneReading = 0;
    int i;
    
    /* read in all process data and populate processes array with the results */
    initializeGlobals();
	while (doneReading=readProcess(&processes[numberOfProcesses]))  {
	   if(doneReading==1)  numberOfProcesses ++;
	   if(numberOfProcesses > MAX_PROCESSES) break;
	}

    /* handle invalid number of processes in input */
    if (numberOfProcesses == 0) {
        fprintf(stderr, "Error: no processes specified in input.\n");
        return -1;
    } else if (numberOfProcesses > MAX_PROCESSES) {
        fprintf(stderr, "Error: too many processes specified in input; "
                        "they cannot number more than %d.\n", MAX_PROCESSES);
        return -1;
    }
    
    /* sort the processes array ascending by arrival time */
    qsort(processes, numberOfProcesses, sizeof(process), compareByArrival);
    
    /* run the simulation */
    while (1) {
        moveIncomingProcesses();  /* admit any newly arriving processes */
        moveRunningProcesses();   /* move procs that shouldn't be running */
        moveWaitingProcesses();   /* move procs finished waiting to ready-Q */
        moveReadyProcesses();     /* move ready procs into any free cpu slots */
        
        updateWaitingProcesses(); /* update burst progress for waiting procs */
        updateReadyProcesses();   /* update waiting time for ready procs */
        updateRunningProcesses(); /* update burst progress for running procs */
        
        cpuTimeUtilized += runningProcesses();

        /* terminate simulation when:
            - no processes are running
            - no more processes await entry into the system
            - there are no waiting processes
        */
        if (runningProcesses() == 0 &&
            incomingProcesses() == 0 &&
            waitingQueue.size == 0) break;

        simulationTime++;
    }
    
     for (i = 0; i < numberOfProcesses; i++)
    	printf("Process #%d: Arrival=%d, Start=%d, End=%d, Waiting=%d\n", 
    		processes[i].pid, processes[i].arrivalTime, processes[i].startTime, processes[i].endTime, processes[i].waitingTime);
    
    /* compute and output performance metrics */
    for (i=0;i<numberOfProcesses;i++) {
        sumOfTurnaroundTimes += processes[i].endTime - processes[i].arrivalTime;
        totalWaitingTime += processes[i].waitingTime;
    }
    printf("totalWaitingTime = %d\nnumberOfProcesses = %d\n", totalWaitingTime, numberOfProcesses);
    printf("Average waiting time                 : %.2f units\n"
           "Average turnaround time              : %.2f units\n"
           "Time all processes finished          : %d\n"
           "Average CPU utilization              : %.1f%%\n"
           "Number of context switches           : %d\n",
           totalWaitingTime / (double) numberOfProcesses,
           sumOfTurnaroundTimes / (double) numberOfProcesses,
           simulationTime,
           100.0 * cpuTimeUtilized / simulationTime,
           totalContextSwitches);
    
    printf("PID(s) of last process(es) to finish :");
    for (i=0;i<numberOfProcesses;i++) {
        if (processes[i].endTime == simulationTime) {
            printf(" %d", processes[i].pid);
        }
    }
    printf("\n");
    return 0;
}