gboolean sawmill_logger_update(gpointer data) { if (sIsAwake) { double rc , c; get_battery_coulomb_reading(&rc, &c); sTimeOnPrint = time_now_ms(); long unsigned int diff_awake = sTimeOnPrint - sTimeOnWake; g_message("%s: raw_coulomb: %f coulomb: %f time_awake_ms: %lu time_asleep_ms: %lu time_screen_on_ms: %lu time_screen_off_ms: %lu", __func__, rc, c, sTotalMSAwake + diff_awake, sTotalMSAsleep, sTotalMSScreenOn + ( sScreenIsOn ? (time_now_ms() - sTimeScreenOn): 0), sTotalMSScreenOff + ( sScreenIsOn ? 0 : (time_now_ms() - sTimeScreenOff)) ); read_proc_loadavg(); read_proc_stat(); read_proc_diskstats(); read_proc_meminfo(); read_proc_net_dev(); } //TODO: use g_timer_source_set_interval(GTimerSource *tsource, guint interval_ms, gboolean from_poll) g_source_remove(sTimerEventSource); sTimerEventSource = g_timeout_add_full(G_PRIORITY_DEFAULT, PRINT_INTERVAL_MS, sawmill_logger_update, GINT_TO_POINTER(TRUE), NULL); return FALSE; }
// Find the PID of the terminal emulator for `target's terminal. // // We assume that the terminal emulator is the parent of the session // leader. This is true in most cases, although in principle you can // construct situations where it is false. We should fail safe later // on if this turns out to be wrong, however. int find_terminal_emulator(struct steal_pty_state *steal) { debug("session leader of pid %d = %d", (int)steal->target_stat.pid, (int)steal->target_stat.sid); struct proc_stat leader_st; int err; if ((err = read_proc_stat(steal->target_stat.sid, &leader_st))) return err; debug("found terminal emulator process: %d", (int) leader_st.ppid); steal->emulator_pid = leader_st.ppid; return 0; }
system_stat get_system_stats(void) { struct timezone tz; system_stat system_stats = read_proc_stat(); system_stats.cpu.cpu_count = sysconf(_SC_NPROCESSORS_ONLN); system_stats.uptime = proc_read_int("/proc/uptime"); system_stats.load_avg = read_load_avg(); system_stats.mem = read_meminfo(); system_stats.sysname = sysname; system_stats.hostname = get_hostname(); gettimeofday(&system_stats.time, &tz); diff_system_stats(&system_stats); return system_stats_old = system_stats; }
int get_terminal_state(struct steal_pty_state *steal, pid_t target) { int err; if ((err = read_proc_stat(target, &steal->target_stat))) return err; if (major(steal->target_stat.ctty) != UNIX98_PTY_SLAVE_MAJOR) { error("Child is not connected to a pseudo-TTY. Unable to steal TTY."); return EINVAL; } if ((err = find_terminal_emulator(steal))) return err; return 0; }
int check_pgroup(pid_t target) { pid_t pg; DIR *dir; struct dirent *d; pid_t pid; char *p; int err = 0; struct proc_stat pid_stat; debug("Checking for problematic process group members..."); pg = getpgid(target); if (pg < 0) { error("Unable to get pgid for pid %d", (int)target); return errno; } if ((dir = opendir("/proc/")) == NULL) return assert_nonzero(errno); while ((d = readdir(dir)) != NULL) { if (d->d_name[0] == '.') continue; pid = strtol(d->d_name, &p, 10); if (*p) continue; if (pid == target) continue; if (getpgid(pid) == pg) { /* * We are actually being somewhat overly-conservative here * -- if pid is a child of target, and has not yet called * execve(), reptyr's setpgid() strategy may suffice. That * is a fairly rare case, and annoying to check for, so * for now let's just bail out. */ if ((err = read_proc_stat(pid, &pid_stat))) { memcpy(pid_stat.comm, "???", 4); } error("Process %d (%.*s) shares %d's process group. Unable to attach.\n" "(This most commonly means that %d has suprocesses).", (int)pid, TASK_COMM_LENGTH, pid_stat.comm, (int)target, (int)target); err = EINVAL; goto out; } } out: closedir(dir); return err; }
int cpu_read_stat(void) { int cpu; if (read_softnet_stat() < 0) return -1; memcpy(&proc_stat_old, &proc_stat, sizeof(proc_stat)); for (cpu = 0; cpu < num_cpus; cpu++) { struct cpu_info *ci = cpu_nth(cpu); if (!ci) continue; memcpy(&ci->ci_psc_old, &ci->ci_psc, sizeof(ci->ci_psc_old)); } if (read_proc_stat(&proc_stat) < 0) return -1; return 0; }
void cpumonitor::on_timeout() { read_proc_stat(); static int32_t count = 0; QIcon sparkLineIcon = dqtx::QSparkLineIconFactory::create( m_cpuUsageHistory, 0, 1, QColor(Qt::blue)); if (count % 2) { m_iconTheme.addIcon(sparkLineIcon, "cpu-sparkline"); m_appIndicator.setIconName("cpu-sparkline"); } else { m_iconTheme.addIcon(sparkLineIcon, "cpu-sparkline-2"); m_appIndicator.setIconName("cpu-sparkline-2"); } ++count; m_appIndicator.show(); }
int main(int argc, char* argv[]) { procstat_t stPreCpuUsage, stCurCpuUsage; procstat_t d; procstat_t u100; /* percentages(%x100) per total */ #ifdef CPU_FREQ cpuinfo_t c0; cpuinfo_t c1; cpuinfo_t c2; cpuinfo_t c3; #endif unsigned long long total, usage100; FILE *ofp = NULL; time_t old, new; clock_t timeStamp = 0; int i = 0; int isFirst = 1; int logInterval = 0; int logTime = 0; int l_pseONOFF = 0; #ifdef PROC_MEM float mem_portion = 0; #endif #ifdef SCORECALC SCORE_RESULT_T stScore = {}; int final_descision = 0; #endif if (argc != 3) { printf("Usage : ./cpu-daemon [interval (ms)] [logging time (s)]\n"); return 0; } for(i = 1; i<argc; i++) { printf("%s ", argv[i]); } logInterval = atoi(argv[1]) * 1000; logTime = atoi(argv[2]); printf("\n v1.3 log Interval = %d[us] log Time = %d[s]\n", logInterval, logTime); time(&old); read_proc_stat(&stPreCpuUsage); ofp = fopen("/data/cpulog/pseON.txt", "rt"); if(ofp == NULL){ printf("fopen open error\n"); return 0; } fscanf(ofp, "%d", &l_pseONOFF); fclose(ofp); ofp = NULL; if((l_pseONOFF == 1)&&(read_scaling_governor() != G_USERSPACE)) { printf("Change to USERSPACE \n"); set_scaling_governor(G_USERSPACE); } while(1) { time(&new); if(difftime(new, old) < logTime) { usleep(logInterval); timeStamp = clock(); read_proc_stat(&stCurCpuUsage); diff_proc_stat(&stCurCpuUsage, &stPreCpuUsage, &d); stPreCpuUsage = stCurCpuUsage; #ifdef CPU_THREAD read_run_thread_number(&c0); #endif #ifdef CPU_FREQ read_cpu0_cur_freq(&c0); read_cpu1_cur_freq(&c1); read_cpu2_cur_freq(&c2); read_cpu3_cur_freq(&c3); #endif #ifdef PROC_MEM mem_portion = get_mem_info(); //printf("PSS : %7.2f%%\n", mem_portion); #endif total = d.user + d.system + d.nice + d.idle + d.iowait + d.irq + d.softirq + d.steal + d.guest + d.guest_nice; u100.user = d.user * 10000 / total; u100.nice = d.nice * 10000 / total; u100.system = d.system * 10000 / total; u100.idle = d.idle * 10000 / total; u100.iowait = d.iowait * 10000 / total; u100.irq = d.irq * 10000 / total; u100.softirq = d.softirq * 10000 / total; u100.steal = d.steal * 10000 / total; u100.guest = d.guest * 10000 / total; u100.guest_nice = d.guest_nice * 10000 / total; usage100 = 10000 - u100.idle; if(isFirst) { isFirst = 0; if (mkdir("/data/cpulog", 775) == -1 && errno != EEXIST) { fprintf(stderr, "Problem creating directory"); perror(" "); } #ifdef SCORECALC //Load each Usage & Score value data from Text files. loadUsageScoreValue(); #endif ofp = fopen("/data/cpulog/cpulog.txt", "w"); if(ofp == NULL) { printf("fopen w error\n"); return 0; } #if defined(CPU_THREAD) && defined(CPU_FREQ) && defined(PROC_MEM) && defined(SCORECALC) fprintf(ofp, "[%5.5s],[%5.5s],[%5.5s], [%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s]\n", "time","cpu","thread", "freq0", "freq1","freq2","freq3", "mem","score", "decision", "tarFreq" ); #elif defined(CPU_THREAD) && defined(CPU_FREQ) && defined(PROC_MEM) fprintf(ofp, "[%5.5s],[%5.5s],[%5.5s], [%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s]\n", "time","cpu","thread", "freq0", "freq1","freq2","freq3", "mem"); #elif defined (CPU_THREAD) && defined(CPU_FREQ) fprintf(ofp, "[%5.5s],[%5.5s],[%5.5s], [%5.5s],[%5.5s],[%5.5s],[%5.5s]\n", "time","cpu","thread", "freq0", "freq1","freq2","freq3"); #else fprintf(ofp, "[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s]\n", "time","cpu","user","nice","system","idle","iowait","irq","softirq","steal","guest","guest_nice"); #endif fclose(ofp); #if defined(CPU_THREAD) && defined(CPU_FREQ) && defined(PROC_MEM) printf("[%5.5s],[%5.5s],[%5.5s],[%5.5s], [%5.5s],[%5.5s],[%5.5s],[%5.5s]\n", "time","cpu","thread","freq0", "freq1","freq2","freq3", "mem"); #elif defined (CPU_THREAD) && defined(CPU_FREQ) printf("[%5.5s],[%5.5s],[%5.5s],[%5.5s], [%5.5s],[%5.5s],[%5.5s]\n", "time","cpu","thread","freq0", "freq1","freq2","freq3"); #else printf( "[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s],[%5.5s]\n", "time","cpu","user","nice","system","idle","iowait","irq","softirq","steal","guest","guest_nice"); #endif } #if defined(CPU_THREAD) && defined(CPU_FREQ) && defined(PROC_MEM) printf("%6.2fs,%4llu.%02llu, %7d,%7d, %7d, %7d, %7d, %4.2f \n", (double)timeStamp/CLOCKS_PER_SEC, usage100/100, usage100%100, c0.run_thread, c0.scaling_cur_freq, c1.scaling_cur_freq, c2.scaling_cur_freq, c3.scaling_cur_freq, mem_portion ); #elif defined (CPU_THREAD) && defined(CPU_FREQ) printf("%6.2fs,%4llu.%02llu, %7d,%7d, %7d, %7d, %7d \n", (double)timeStamp/CLOCKS_PER_SEC, usage100/100, usage100%100, c0.run_thread, c0.scaling_cur_freq, c1.scaling_cur_freq, c2.scaling_cur_freq, c3.scaling_cur_freq ); #else printf("%6.2fs,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu\n", (double)timeStamp/CLOCKS_PER_SEC, usage100/100, usage100%100, u100.user/100, u100.user%100, u100.nice/100, u100.nice%100, u100.system/100, u100.system%100, u100.idle/100, u100.idle%100, u100.iowait/100, u100.iowait%100, u100.irq/100, u100.irq%100, u100.softirq/100, u100.softirq%100, u100.steal/100, u100.steal%100, u100.guest/100, u100.guest%100, u100.guest_nice/100, u100.guest_nice%100 ); #endif ofp = fopen("/data/cpulog/cpulog.txt", "a"); if(ofp == NULL) { printf("fopen a error\n"); return 0; } #ifdef SCORECALC RESOURCE_USAGE_T stResourceUsage; stResourceUsage.cpuUsage = (float)usage100/100; stResourceUsage.threadUsage = c0.run_thread; stResourceUsage.memoryUsage = mem_portion; stResourceUsage.cpuFreq = c0.scaling_cur_freq/1000; stScore = calcResourceScore(&stResourceUsage); #endif #if defined(CPU_THREAD) && defined(CPU_FREQ) && defined(SCORECALC) fprintf(ofp, "%6.2fs,%4llu.%02llu, %7d, %7d, %7d, %7d, %7d, %4.2f,%7d,%7d,%7d \n", (double)timeStamp/CLOCKS_PER_SEC, usage100/100, usage100%100, c0.run_thread, c0.scaling_cur_freq, c1.scaling_cur_freq, c2.scaling_cur_freq, c3.scaling_cur_freq, mem_portion, stScore.score, stScore.finalDecision, stScore.targetFreq ); #elif defined(CPU_THREAD) && defined(CPU_FREQ) fprintf(ofp, "%6.2fs,%4llu.%02llu, %7d, %7d, %7d, %7d, %7d, %4.2f \n", (double)timeStamp/CLOCKS_PER_SEC, usage100/100, usage100%100, c0.run_thread, c0.scaling_cur_freq, c1.scaling_cur_freq, c2.scaling_cur_freq, c3.scaling_cur_freq, mem_portion ); #elif defined (CPU_THREAD) && defined(CPU_FREQ) fprintf(ofp, "%6.2fs,%4llu.%02llu, %7d, %7d, %7d, %7d, %7d \n", (double)timeStamp/CLOCKS_PER_SEC, usage100/100, usage100%100, c0.run_thread, c0.scaling_cur_freq, c1.scaling_cur_freq, c2.scaling_cur_freq, c3.scaling_cur_freq ); #else fprintf(ofp, "%6.2fs,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu,%4llu.%02llu\n", (double)timeStamp/CLOCKS_PER_SEC, usage100/100, usage100%100, u100.user/100, u100.user%100, u100.nice/100, u100.nice%100, u100.system/100, u100.system%100, u100.idle/100, u100.idle%100, u100.iowait/100, u100.iowait%100, u100.irq/100, u100.irq%100, u100.softirq/100, u100.softirq%100, u100.steal/100, u100.steal%100, u100.guest/100, u100.guest%100, u100.guest_nice/100, u100.guest_nice%100 ); #endif fclose(ofp); } else { printf("cpu usage logging finished\n"); if(read_scaling_governor() != G_ONDEMAND) { printf("Come back to ONDEMAND \n"); set_scaling_governor(G_ONDEMAND); } return 0; } }
cpumonitor::cpumonitor() : m_appIndicator( "dqtx-cpu-monitor", "", "", m_iconTheme.dir().absolutePath()) { read_proc_stat(); }
/* Name: GetCPUStats (Externally exposed function - see header file) * Description: - * Parameters: - * Returns: - * Side effects: - * Notes: This code has a STUB in it. */ struct cpustats *GetCPUStats(struct cpustats *cpus) { char line[256]; struct bufhandle bh; void *readbuf; unsigned long bufsize; int cpulncnt = 0; int index_cpus = 0; /* Should we index the CPUs (or not) */ //STUB fprintf(stderr, "GetCPUStats("); //STUB fprintf(stderr, "1"); /* Check for NULL input - and handle the buffer part ONLY */ if ( NULL == cpus ) { /* Determine how big the buffer needs to be */ if ( 0 == (bufsize = read_proc_stat(NULL, 0)) ) return(NULL); /* Allocate some memory for it based on what we just found */ if ( NULL == (readbuf = malloc(bufsize)) ) return(NULL); } else { readbuf = cpus->buf; /* Get the already allocated buffer */ bufsize = cpus->bufsize; /* Get the size of the buffer */ cpus->isvalid = 0; /* Consider invalid until done */ } //STUBfprintf(stderr, "2"); /* So now readbuf is good, and the bufsize is good, read in actual data */ if ( 0 == read_proc_stat(readbuf, bufsize) ) return(cpus); /* If cpus struct is null - then allocate it */ if ( cpus == NULL ) { /* Set up our buffer pointers */ bh.buf = readbuf; bh.readptr = readbuf; /* Make a note to index the CPU list */ index_cpus = 1; /* Read data out of the buffer - just to count CPU lines */ while(get_cpu_line(line, 256, &bh)) { cpulncnt++; } /* Allocate the cpustats structure */ if ( NULL == (cpus = alloc_cpustats(cpulncnt)) ) { free(readbuf); /* Do this because we know it does not belong to a cpustats struct */ return(NULL); } /* Add-in our previously allocated read buffer */ cpus->buf = readbuf; cpus->bufsize = bufsize; /* Mark this read as invalid - as we are not done at this time */ cpus->isvalid = 0; } /* Now start reading data into the cpustats struct */ bh.buf = readbuf; bh.readptr = readbuf; while(get_cpu_line(line, 256, &bh)) { parse_cpu_line(cpus, line); } /* STUB: Probably should be validating against the results of parse_cpu_line() */ cpus->isvalid = 0; #ifdef STUB_DEBUG if ( index_cpus ) { if (build_cpu_index(cpus)) { /* This will be our first time through. All memory has been successfully allocated, so it must all be freed. */ /* STUB: Call free_all_cpus() - after you write it */ return(NULL); } } #endif //STUB fprintf(stderr, ");\n"); return(cpus); }
/* * Read the proc statistics information of given group name. * * IN: * @group_name : The given group name. * @stats_lists: The lists of proc statistics information. * * RETURN * 0 Success. * -1 Failed. */ int read_cg_proc_stats(const char* group_name, cg_proc_stats_lists* stats_lists) { FILE *fp = NULL; int i = 0, j = 0, num = 0; char* pre = NULL; pid_t* procs; char proc_name[FN_LEN] = { 0 }; char line[MAX_LINE_LEN] = { 0 }; cg_proc_stats* cg_stats = NULL; if (!group_name || !stats_lists) { #ifdef DEBUG fprintf(stderr, "rd_stats: the group name or io stats lists is null.\n"); #endif return -1; } /** Clear the history information.*/ clear_proc_stats(stats_lists); /** Get all of the process id in the group. But the variables procs should * be free at the end of this procedure.*/ if (cgroup_get_procs(group_name, CG_CPUSET, &procs, &j)) { #ifdef DEBUG fprintf(stderr, "rd_stats: read the value of procs in cpuset %s failed\n", group_name); #endif return -1; } /** Read the io statistics information of each process id(/proc/<PID>/io). * Initialized the io statistics information and store the value of the * parameter. Finally add the io statistics information into the lists.*/ for (i = 0; i < j; i++) { /** Initialized the io statistics information.*/ if (init_proc_stats(&cg_stats, procs[i])) { fprintf(stderr, "rd_stats: initialize the io stats failed\n"); goto err; } if (read_proc_io(cg_stats->io_stats, procs[i]) || read_proc_stat(cg_stats->proc_stat, procs[i]) || read_proc_statm(cg_stats->proc_statm, procs[i])) { #ifdef DEBUG fprintf(stderr, "rd_stats: read the proc io stats, proc" " stat and proc statm into io stats list failed\n"); #endif goto err; } /** Add the io statistics information into the lists.*/ if (add_proc_stats(stats_lists, cg_stats)) { #ifdef DEBUG fprintf(stderr, "rd_stats: add the io stats into io stats list failed\n"); #endif goto err; } } if (procs) free(procs); return 0; err: if (cg_stats) uninit_proc_stats(cg_stats); clear_proc_stats(stats_lists); if (procs) free(procs); if (fp) fclose(fp); return -1; }
void COSADmlGetProcessInfo(PCOSA_DATAMODEL_PROCSTATUS p_info) { PCOSA_PROCESS_ENTRY p_proc = NULL; static ULONG ProcessTimeStamp; ULONG ProcessNumber = 0; struct dirent *entry; DIR *dir; FILE *fp; char* name; int num; int i; ULONG pid; char status[32]; char buf[400]; ULONG utime; ULONG stime; char state[64]; dir = opendir("/proc"); if ( !dir ) { CcspTraceWarning(("Failed to open /proc!\n")); return ; } for(;;) { if ( (entry = readdir(dir)) == NULL ) { closedir(dir); dir = NULL; break; } name = entry->d_name; if ( *name >= '0' && *name <= '9' ) { ProcessNumber++; } } /*CcspTraceWarning(("ProcessNumber = %d!\n", ProcessNumber));*/ p_info->pProcTable = AnscAllocateMemory(sizeof(COSA_PROCESS_ENTRY) * ProcessNumber); if( !p_info->pProcTable ) { return ; } AnscZeroMemory(p_info->pProcTable, sizeof(COSA_PROCESS_ENTRY) * ProcessNumber); p_info->ProcessNumberOfEntries = ProcessNumber; dir = opendir("/proc"); if ( !dir ) { CcspTraceWarning(("Failed to open /proc!\n")); return ; } for(i = 0; i < ProcessNumber; ) { if ( (entry = readdir(dir)) == NULL ) { closedir(dir); dir = NULL; break; } name = entry->d_name; if ( *name >= '0' && *name <= '9' ) { /*CcspTraceWarning(("Begin to parse process %lu!", i));*/ p_proc = p_info->pProcTable+i; i++; pid = atoi(name); p_proc->Pid = pid; sprintf(status, "/proc/%lu/stat", pid); if ( !(fp = fopen(status, "r")) ) { CcspTraceWarning(("Failed to open %s!\n", status)); continue; } name = fgets(buf, sizeof(buf), fp); fclose(fp); if ( !name ) { CcspTraceWarning(("Failed to get process %d information!\n", pid)); continue; } memset(state, 0, sizeof(state)); if (read_proc_stat(name, p_proc->Command, &state, &p_proc->Size, &p_proc->Priority, &p_proc->CPUTime )) { CcspTraceWarning(("Failed to parse process %d information!\n", pid)); continue; } /*CcspTraceWarning((" Cmd:%s, size, priority, cputime %d:%d:%d \n", p_proc->Command, p_proc->Size, p_proc->Priority, p_proc->CPUTime));*/ name = strchr(p_proc->Command, ')'); if ( name ) { *name = '\0'; } switch (*state) { case 'R': p_proc->State = COSA_DML_PROC_STATUS_Running; break; case 'S': p_proc->State = COSA_DML_PROC_STATUS_Sleeping; break; case 'D': p_proc->State = COSA_DML_PROC_STATUS_Uninterruptible; break; case 'T': p_proc->State = COSA_DML_PROC_STATUS_Stopped; break; case 'Z': case 'X': p_proc->State = COSA_DML_PROC_STATUS_Zombie; break; default: p_proc->State = COSA_DML_PROC_STATUS_Idle; } } } if ( i != p_info->ProcessNumberOfEntries ) { p_info->ProcessNumberOfEntries = i; } return; }