From ff9409b1737627857eb47f64f536a3f66b6a09a4 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 11 Apr 2016 13:01:07 +0200 Subject: Imported Upstream version 2.0.0 --- freebsd/Battery.c | 15 ++ freebsd/Battery.h | 15 ++ freebsd/FreeBSDCRT.c | 21 ++ freebsd/FreeBSDCRT.h | 15 ++ freebsd/FreeBSDProcess.c | 164 +++++++++++++ freebsd/FreeBSDProcess.h | 54 +++++ freebsd/FreeBSDProcessList.c | 532 +++++++++++++++++++++++++++++++++++++++++++ freebsd/FreeBSDProcessList.h | 69 ++++++ freebsd/Platform.c | 206 +++++++++++++++++ freebsd/Platform.h | 51 +++++ 10 files changed, 1142 insertions(+) create mode 100644 freebsd/Battery.c create mode 100644 freebsd/Battery.h create mode 100644 freebsd/FreeBSDCRT.c create mode 100644 freebsd/FreeBSDCRT.h create mode 100644 freebsd/FreeBSDProcess.c create mode 100644 freebsd/FreeBSDProcess.h create mode 100644 freebsd/FreeBSDProcessList.c create mode 100644 freebsd/FreeBSDProcessList.h create mode 100644 freebsd/Platform.c create mode 100644 freebsd/Platform.h (limited to 'freebsd') diff --git a/freebsd/Battery.c b/freebsd/Battery.c new file mode 100644 index 0000000..ab63aa1 --- /dev/null +++ b/freebsd/Battery.c @@ -0,0 +1,15 @@ +/* +htop - freebsd/Battery.c +(C) 2015 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "BatteryMeter.h" + +void Battery_getData(double* level, ACPresence* isOnAC) { + // TODO + *level = -1; + *isOnAC = AC_ERROR; +} + diff --git a/freebsd/Battery.h b/freebsd/Battery.h new file mode 100644 index 0000000..2b4a9a8 --- /dev/null +++ b/freebsd/Battery.h @@ -0,0 +1,15 @@ +/* Do not edit this file. It was automatically generated. */ + +#ifndef HEADER_Battery +#define HEADER_Battery +/* +htop - freebsd/Battery.h +(C) 2015 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +void Battery_getData(double* level, ACPresence* isOnAC); + + +#endif diff --git a/freebsd/FreeBSDCRT.c b/freebsd/FreeBSDCRT.c new file mode 100644 index 0000000..5c3a9de --- /dev/null +++ b/freebsd/FreeBSDCRT.c @@ -0,0 +1,21 @@ +/* +htop - UnsupportedCRT.c +(C) 2014 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" +#include "CRT.h" +#include +#include + +void CRT_handleSIGSEGV(int sgn) { + (void) sgn; + CRT_done(); + fprintf(stderr, "\n\nhtop " VERSION " aborting.\n"); + fprintf(stderr, "\nUnfortunately, you seem to be using an unsupported platform!"); + fprintf(stderr, "\nPlease contact your platform package maintainer!\n\n"); + abort(); +} + diff --git a/freebsd/FreeBSDCRT.h b/freebsd/FreeBSDCRT.h new file mode 100644 index 0000000..eb1091a --- /dev/null +++ b/freebsd/FreeBSDCRT.h @@ -0,0 +1,15 @@ +/* Do not edit this file. It was automatically generated. */ + +#ifndef HEADER_FreeBSDCRT +#define HEADER_FreeBSDCRT +/* +htop - UnsupportedCRT.h +(C) 2014 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +void CRT_handleSIGSEGV(int sgn); + + +#endif diff --git a/freebsd/FreeBSDProcess.c b/freebsd/FreeBSDProcess.c new file mode 100644 index 0000000..70cfb95 --- /dev/null +++ b/freebsd/FreeBSDProcess.c @@ -0,0 +1,164 @@ +/* +htop - FreeBSDProcess.c +(C) 2015 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Process.h" +#include "ProcessList.h" +#include "FreeBSDProcess.h" +#include "Platform.h" +#include "CRT.h" + +#include +#include +#include +#include + +/*{ + +typedef enum FreeBSDProcessFields { + // Add platform-specific fields here, with ids >= 100 + JID = 100, + JAIL = 101, + LAST_PROCESSFIELD = 102, +} FreeBSDProcessField; + + +typedef struct FreeBSDProcess_ { + Process super; + int kernel; + int jid; + char* jname; +} FreeBSDProcess; + + +#ifndef Process_isKernelThread +#define Process_isKernelThread(_process) (_process->kernel == 1) +#endif + +#ifndef Process_isUserlandThread +#define Process_isUserlandThread(_process) (_process->pid != _process->tgid) +#endif + +}*/ + +ProcessClass FreeBSDProcess_class = { + .super = { + .extends = Class(Process), + .display = Process_display, + .delete = Process_delete, + .compare = FreeBSDProcess_compare + }, + .writeField = (Process_WriteField) FreeBSDProcess_writeField, +}; + +ProcessFieldData Process_fields[] = { + [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, + [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, + [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, + [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging)", .flags = 0, }, + [PPID] = { .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, }, + [PGRP] = { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, }, + [SESSION] = { .name = "SESSION", .title = " SESN ", .description = "Process's session ID", .flags = 0, }, + [TTY_NR] = { .name = "TTY_NR", .title = " TTY ", .description = "Controlling terminal", .flags = 0, }, + [TPGID] = { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, }, + [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, + [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, + [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, }, + [NICE] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, }, + [STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, }, + + [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, }, + [M_SIZE] = { .name = "M_SIZE", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, }, + [M_RESIDENT] = { .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, }, + [ST_UID] = { .name = "ST_UID", .title = " UID ", .description = "User ID of the process owner", .flags = 0, }, + [PERCENT_CPU] = { .name = "PERCENT_CPU", .title = "CPU% ", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, }, + [PERCENT_MEM] = { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, }, + [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, }, + [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, }, + [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, + [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, + [JID] = { .name = "JID", .title = " JID ", .description = "Jail prison ID", .flags = 0, }, + [JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, }, + [LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, +}; + +ProcessPidColumn Process_pidColumns[] = { + { .id = JID, .label = "JID" }, + { .id = PID, .label = "PID" }, + { .id = PPID, .label = "PPID" }, + { .id = TPGID, .label = "TPGID" }, + { .id = TGID, .label = "TGID" }, + { .id = PGRP, .label = "PGRP" }, + { .id = SESSION, .label = "SESN" }, + { .id = 0, .label = NULL }, +}; + +FreeBSDProcess* FreeBSDProcess_new(Settings* settings) { + FreeBSDProcess* this = xCalloc(1, sizeof(FreeBSDProcess)); + Object_setClass(this, Class(FreeBSDProcess)); + Process_init(&this->super, settings); + return this; +} + +void Process_delete(Object* cast) { + FreeBSDProcess* this = (FreeBSDProcess*) cast; + Process_done((Process*)cast); + free(this->jname); + free(this); +} + +void FreeBSDProcess_writeField(Process* this, RichString* str, ProcessField field) { + FreeBSDProcess* fp = (FreeBSDProcess*) this; + char buffer[256]; buffer[255] = '\0'; + int attr = CRT_colors[DEFAULT_COLOR]; + int n = sizeof(buffer) - 1; + switch (field) { + // add FreeBSD-specific fields here + case JID: snprintf(buffer, n, Process_pidFormat, fp->jid); break; + case JAIL:{ + snprintf(buffer, n, "%-11s ", fp->jname); break; + if (buffer[11] != '\0') { + buffer[11] = ' '; + buffer[12] = '\0'; + } + break; + } + default: + Process_writeField(this, str, field); + return; + } + RichString_append(str, attr, buffer); +} + +long FreeBSDProcess_compare(const void* v1, const void* v2) { + FreeBSDProcess *p1, *p2; + Settings *settings = ((Process*)v1)->settings; + if (settings->direction == 1) { + p1 = (FreeBSDProcess*)v1; + p2 = (FreeBSDProcess*)v2; + } else { + p2 = (FreeBSDProcess*)v1; + p1 = (FreeBSDProcess*)v2; + } + switch (settings->sortKey) { + // add FreeBSD-specific fields here + case JID: + return (p1->jid - p2->jid); + case JAIL: + return strcmp(p1->jname ? p1->jname : "", p2->jname ? p2->jname : ""); + default: + return Process_compare(v1, v2); + } +} + +bool Process_isThread(Process* this) { + FreeBSDProcess* fp = (FreeBSDProcess*) this; + + if (fp->kernel == 1 ) + return 1; + else + return (Process_isUserlandThread(this)); +} diff --git a/freebsd/FreeBSDProcess.h b/freebsd/FreeBSDProcess.h new file mode 100644 index 0000000..23d298a --- /dev/null +++ b/freebsd/FreeBSDProcess.h @@ -0,0 +1,54 @@ +/* Do not edit this file. It was automatically generated. */ + +#ifndef HEADER_FreeBSDProcess +#define HEADER_FreeBSDProcess +/* +htop - FreeBSDProcess.h +(C) 2015 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + + +typedef enum FreeBSDProcessFields { + // Add platform-specific fields here, with ids >= 100 + JID = 100, + JAIL = 101, + LAST_PROCESSFIELD = 102, +} FreeBSDProcessField; + + +typedef struct FreeBSDProcess_ { + Process super; + int kernel; + int jid; + char* jname; +} FreeBSDProcess; + + +#ifndef Process_isKernelThread +#define Process_isKernelThread(_process) (_process->kernel == 1) +#endif + +#ifndef Process_isUserlandThread +#define Process_isUserlandThread(_process) (_process->pid != _process->tgid) +#endif + + +extern ProcessClass FreeBSDProcess_class; + +extern ProcessFieldData Process_fields[]; + +extern ProcessPidColumn Process_pidColumns[]; + +FreeBSDProcess* FreeBSDProcess_new(Settings* settings); + +void Process_delete(Object* cast); + +void FreeBSDProcess_writeField(Process* this, RichString* str, ProcessField field); + +long FreeBSDProcess_compare(const void* v1, const void* v2); + +bool Process_isThread(Process* this); + +#endif diff --git a/freebsd/FreeBSDProcessList.c b/freebsd/FreeBSDProcessList.c new file mode 100644 index 0000000..7c4a6db --- /dev/null +++ b/freebsd/FreeBSDProcessList.c @@ -0,0 +1,532 @@ +/* +htop - FreeBSDProcessList.c +(C) 2014 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "ProcessList.h" +#include "FreeBSDProcessList.h" +#include "FreeBSDProcess.h" + +#include +#include +#include +#include +#include +#include +#include + +/*{ + +#include +#include +#include +#include +#include + +#define JAIL_ERRMSGLEN 1024 +char jail_errmsg[JAIL_ERRMSGLEN]; + +typedef struct CPUData_ { + + double userPercent; + double nicePercent; + double systemPercent; + double irqPercent; + double idlePercent; + double systemAllPercent; + +} CPUData; + +typedef struct FreeBSDProcessList_ { + ProcessList super; + kvm_t* kd; + + int zfsArcEnabled; + + unsigned long long int memWire; + unsigned long long int memActive; + unsigned long long int memInactive; + unsigned long long int memFree; + unsigned long long int memZfsArc; + + + CPUData* cpus; + + unsigned long *cp_time_o; + unsigned long *cp_time_n; + + unsigned long *cp_times_o; + unsigned long *cp_times_n; + +} FreeBSDProcessList; + +}*/ + + +static int MIB_hw_physmem[2]; +static int MIB_vm_stats_vm_v_page_count[4]; +static int pageSize; +static int pageSizeKb; + +static int MIB_vm_stats_vm_v_wire_count[4]; +static int MIB_vm_stats_vm_v_active_count[4]; +static int MIB_vm_stats_vm_v_cache_count[4]; +static int MIB_vm_stats_vm_v_inactive_count[4]; +static int MIB_vm_stats_vm_v_free_count[4]; + +static int MIB_vfs_bufspace[2]; + +static int MIB_kstat_zfs_misc_arcstats_size[5]; + +static int MIB_kern_cp_time[2]; +static int MIB_kern_cp_times[2]; +static int kernelFScale; + + +ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { + FreeBSDProcessList* fpl = xCalloc(1, sizeof(FreeBSDProcessList)); + ProcessList* pl = (ProcessList*) fpl; + ProcessList_init(pl, Class(FreeBSDProcess), usersTable, pidWhiteList, userId); + + size_t len; + + // physical memory in system: hw.physmem + // physical page size: hw.pagesize + // usable pagesize : vm.stats.vm.v_page_size + len = 2; sysctlnametomib("hw.physmem", MIB_hw_physmem, &len); + + len = sizeof(pageSize); + if (sysctlbyname("vm.stats.vm.v_page_size", &pageSize, &len, NULL, 0) == -1) { + pageSize = PAGE_SIZE; + pageSizeKb = PAGE_SIZE_KB; + } else { + pageSizeKb = pageSize / ONE_K; + } + + // usable page count vm.stats.vm.v_page_count + // actually usable memory : vm.stats.vm.v_page_count * vm.stats.vm.v_page_size + len = 4; sysctlnametomib("vm.stats.vm.v_page_count", MIB_vm_stats_vm_v_page_count, &len); + + len = 4; sysctlnametomib("vm.stats.vm.v_wire_count", MIB_vm_stats_vm_v_wire_count, &len); + len = 4; sysctlnametomib("vm.stats.vm.v_active_count", MIB_vm_stats_vm_v_active_count, &len); + len = 4; sysctlnametomib("vm.stats.vm.v_cache_count", MIB_vm_stats_vm_v_cache_count, &len); + len = 4; sysctlnametomib("vm.stats.vm.v_inactive_count", MIB_vm_stats_vm_v_inactive_count, &len); + len = 4; sysctlnametomib("vm.stats.vm.v_free_count", MIB_vm_stats_vm_v_free_count, &len); + + len = 2; sysctlnametomib("vfs.bufspace", MIB_vfs_bufspace, &len); + + len = sizeof(fpl->memZfsArc); + if (sysctlbyname("kstat.zfs.misc.arcstats.size", &fpl->memZfsArc, &len, + NULL, 0) == 0 && fpl->memZfsArc != 0) { + sysctlnametomib("kstat.zfs.misc.arcstats.size", MIB_kstat_zfs_misc_arcstats_size, &len); + fpl->zfsArcEnabled = 1; + } else { + fpl->zfsArcEnabled = 0; + } + + + int smp = 0; + len = sizeof(smp); + + if (sysctlbyname("kern.smp.active", &smp, &len, NULL, 0) != 0 || len != sizeof(smp)) { + smp = 0; + } + + int cpus = 1; + len = sizeof(cpus); + + if (smp) { + int err = sysctlbyname("kern.smp.cpus", &cpus, &len, NULL, 0); + if (err) cpus = 1; + } else { + cpus = 1; + } + + size_t sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES; + len = 2; sysctlnametomib("kern.cp_time", MIB_kern_cp_time, &len); + fpl->cp_time_o = xCalloc(cpus, sizeof_cp_time_array); + fpl->cp_time_n = xCalloc(cpus, sizeof_cp_time_array); + len = sizeof_cp_time_array; + + // fetch intial single (or average) CPU clicks from kernel + sysctl(MIB_kern_cp_time, 2, fpl->cp_time_o, &len, NULL, 0); + + // on smp box, fetch rest of intial CPU's clicks + if (cpus > 1) { + len = 2; sysctlnametomib("kern.cp_times", MIB_kern_cp_times, &len); + fpl->cp_times_o = xCalloc(cpus, sizeof_cp_time_array); + fpl->cp_times_n = xCalloc(cpus, sizeof_cp_time_array); + len = cpus * sizeof_cp_time_array; + sysctl(MIB_kern_cp_times, 2, fpl->cp_times_o, &len, NULL, 0); + } + + pl->cpuCount = MAX(cpus, 1); + + if (cpus == 1 ) { + fpl->cpus = xRealloc(fpl->cpus, sizeof(CPUData)); + } else { + // on smp we need CPUs + 1 to store averages too (as kernel kindly provides that as well) + fpl->cpus = xRealloc(fpl->cpus, (pl->cpuCount + 1) * sizeof(CPUData)); + } + + + len = sizeof(kernelFScale); + if (sysctlbyname("kern.fscale", &kernelFScale, &len, NULL, 0) == -1) { + //sane default for kernel provded CPU precentage scaling, at least on x86 machines, in case this sysctl call failed + kernelFScale = 2048; + } + + fpl->kd = kvm_open(NULL, "/dev/null", NULL, 0, NULL); + assert(fpl->kd); + + return pl; +} + +void ProcessList_delete(ProcessList* this) { + const FreeBSDProcessList* fpl = (FreeBSDProcessList*) this; + if (fpl->kd) kvm_close(fpl->kd); + + free(fpl->cp_time_o); + free(fpl->cp_time_n); + free(fpl->cp_times_o); + free(fpl->cp_times_n); + free(fpl->cpus); + + ProcessList_done(this); + free(this); +} + +static inline void FreeBSDProcessList_scanCPUTime(ProcessList* pl) { + const FreeBSDProcessList* fpl = (FreeBSDProcessList*) pl; + + int cpus = pl->cpuCount; // actual CPU count + int maxcpu = cpus; // max iteration (in case we have average + smp) + int cp_times_offset; + + assert(cpus > 0); + + size_t sizeof_cp_time_array; + + unsigned long *cp_time_n; // old clicks state + unsigned long *cp_time_o; // current clicks state + + unsigned long long total_o = 0; + unsigned long long total_n = 0; + unsigned long long total_d = 0; + unsigned long cp_time_d[CPUSTATES]; + double cp_time_p[CPUSTATES]; + + // get averages or single CPU clicks + sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES; + sysctl(MIB_kern_cp_time, 2, fpl->cp_time_n, &sizeof_cp_time_array, NULL, 0); + + // get rest of CPUs + if (cpus > 1) { + // on smp systems FreeBSD kernel concats all CPU states into one long array in + // kern.cp_times sysctl OID + // we store averages in fpl->cpus[0], and actual cores after that + maxcpu = cpus + 1; + sizeof_cp_time_array = cpus * sizeof(unsigned long) * CPUSTATES; + sysctl(MIB_kern_cp_times, 2, fpl->cp_times_n, &sizeof_cp_time_array, NULL, 0); + } + + for (int i = 0; i < maxcpu; i++) { + if (cpus == 1) { + // single CPU box + cp_time_n = fpl->cp_time_n; + cp_time_o = fpl->cp_time_o; + } else { + if (i == 0 ) { + // average + cp_time_n = fpl->cp_time_n; + cp_time_o = fpl->cp_time_o; + } else { + // specific smp cores + cp_times_offset = i - 1; + cp_time_n = fpl->cp_times_n + (cp_times_offset * CPUSTATES); + cp_time_o = fpl->cp_times_o + (cp_times_offset * CPUSTATES); + } + } + + // diff old vs new + for (int s = 0; s < CPUSTATES; s++) { + cp_time_d[s] = cp_time_n[s] - cp_time_o[s]; + total_o += cp_time_o[s]; + total_n += cp_time_n[s]; + } + + // totals + total_d = total_n - total_o; + if (total_d < 1 ) total_d = 1; + + // save current state as old and calc percentages + for (int s = 0; s < CPUSTATES; ++s) { + cp_time_o[s] = cp_time_n[s]; + cp_time_p[s] = ((double)cp_time_d[s]) / ((double)total_d) * 100; + } + + CPUData* cpuData = &(fpl->cpus[i]); + cpuData->userPercent = cp_time_p[CP_USER]; + cpuData->nicePercent = cp_time_p[CP_NICE]; + cpuData->systemPercent = cp_time_p[CP_SYS]; + cpuData->irqPercent = cp_time_p[CP_INTR]; + cpuData->systemAllPercent = cp_time_p[CP_SYS] + cp_time_p[CP_INTR]; + // this one is not really used, but we store it anyway + cpuData->idlePercent = cp_time_p[CP_IDLE]; + } +} + +static inline void FreeBSDProcessList_scanMemoryInfo(ProcessList* pl) { + FreeBSDProcessList* fpl = (FreeBSDProcessList*) pl; + + // @etosan: + // memory counter relationships seem to be these: + // total = active + wired + inactive + cache + free + // htop_used (unavail to anybody) = active + wired + // htop_cache (for cache meter) = buffers + cache + // user_free (avail to procs) = buffers + inactive + cache + free + // + // with ZFS ARC situation becomes bit muddled, as ARC behaves like "user_free" + // and belongs into cache, but is reported as wired by kernel + // + // htop_used = active + (wired - arc) + // htop_cache = buffers + cache + arc + size_t len = sizeof(pl->totalMem); + + //disabled for now, as it is always smaller than phycal amount of memory... + //...to avoid "where is my memory?" questions + //sysctl(MIB_vm_stats_vm_v_page_count, 4, &(pl->totalMem), &len, NULL, 0); + //pl->totalMem *= pageSizeKb; + sysctl(MIB_hw_physmem, 2, &(pl->totalMem), &len, NULL, 0); + pl->totalMem /= 1024; + + sysctl(MIB_vm_stats_vm_v_active_count, 4, &(fpl->memActive), &len, NULL, 0); + fpl->memActive *= pageSizeKb; + + sysctl(MIB_vm_stats_vm_v_wire_count, 4, &(fpl->memWire), &len, NULL, 0); + fpl->memWire *= pageSizeKb; + + sysctl(MIB_vfs_bufspace, 2, &(pl->buffersMem), &len, NULL, 0); + pl->buffersMem /= 1024; + + sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(pl->cachedMem), &len, NULL, 0); + pl->cachedMem *= pageSizeKb; + + if (fpl->zfsArcEnabled) { + len = sizeof(fpl->memZfsArc); + sysctl(MIB_kstat_zfs_misc_arcstats_size, 5, &(fpl->memZfsArc), &len , NULL, 0); + fpl->memZfsArc /= 1024; + fpl->memWire -= fpl->memZfsArc; + pl->cachedMem += fpl->memZfsArc; + // maybe when we learn how to make custom memory meter + // we could do custom arc breakdown? + } + + pl->usedMem = fpl->memActive + fpl->memWire; + + //currently unused, same as with arc, custom meter perhaps + //sysctl(MIB_vm_stats_vm_v_inactive_count, 4, &(fpl->memInactive), &len, NULL, 0); + //sysctl(MIB_vm_stats_vm_v_free_count, 4, &(fpl->memFree), &len, NULL, 0); + //pl->freeMem = fpl->memInactive + fpl->memFree; + //pl->freeMem *= pageSizeKb; + + struct kvm_swap swap[16]; + int nswap = kvm_getswapinfo(fpl->kd, swap, sizeof(swap)/sizeof(swap[0]), 0); + pl->totalSwap = 0; + pl->usedSwap = 0; + for (int i = 0; i < nswap; i++) { + pl->totalSwap += swap[i].ksw_total; + pl->usedSwap += swap[i].ksw_used; + } + pl->totalSwap *= pageSizeKb; + pl->usedSwap *= pageSizeKb; + + pl->sharedMem = 0; // currently unused +} + +char* FreeBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) { + char** argv = kvm_getargv(kd, kproc, 0); + if (!argv) { + return xStrdup(kproc->ki_comm); + } + int len = 0; + for (int i = 0; argv[i]; i++) { + len += strlen(argv[i]) + 1; + } + char* comm = xMalloc(len); + char* at = comm; + *basenameEnd = 0; + for (int i = 0; argv[i]; i++) { + at = stpcpy(at, argv[i]); + if (!*basenameEnd) { + *basenameEnd = at - comm; + } + *at = ' '; + at++; + } + at--; + *at = '\0'; + return comm; +} + +char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) { + int jid; + struct iovec jiov[6]; + char* jname; + char jnamebuf[MAXHOSTNAMELEN]; + + if (kproc->ki_jid != 0 ){ + memset(jnamebuf, 0, sizeof(jnamebuf)); + *(const void **)&jiov[0].iov_base = "jid"; + jiov[0].iov_len = sizeof("jid"); + jiov[1].iov_base = &kproc->ki_jid; + jiov[1].iov_len = sizeof(kproc->ki_jid); + *(const void **)&jiov[2].iov_base = "name"; + jiov[2].iov_len = sizeof("name"); + jiov[3].iov_base = jnamebuf; + jiov[3].iov_len = sizeof(jnamebuf); + *(const void **)&jiov[4].iov_base = "errmsg"; + jiov[4].iov_len = sizeof("errmsg"); + jiov[5].iov_base = jail_errmsg; + jiov[5].iov_len = JAIL_ERRMSGLEN; + jail_errmsg[0] = 0; + jid = jail_get(jiov, 6, 0); + if (jid < 0) { + if (!jail_errmsg[0]) + snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", strerror(errno)); + return NULL; + } else if (jid == kproc->ki_jid) { + jname = xStrdup(jnamebuf); + if (jname == NULL) + strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); + return jname; + } else { + return NULL; + } + } else { + jnamebuf[0]='-'; + jnamebuf[1]='\0'; + jname = xStrdup(jnamebuf); + } + return jname; +} + +void ProcessList_goThroughEntries(ProcessList* this) { + FreeBSDProcessList* fpl = (FreeBSDProcessList*) this; + Settings* settings = this->settings; + bool hideKernelThreads = settings->hideKernelThreads; + bool hideUserlandThreads = settings->hideUserlandThreads; + + FreeBSDProcessList_scanMemoryInfo(this); + FreeBSDProcessList_scanCPUTime(this); + + int cpus = this->cpuCount; + int count = 0; + struct kinfo_proc* kprocs = kvm_getprocs(fpl->kd, KERN_PROC_ALL, 0, &count); + + for (int i = 0; i < count; i++) { + struct kinfo_proc* kproc = &kprocs[i]; + bool preExisting = false; + bool isIdleProcess = false; + Process* proc = ProcessList_getProcess(this, kproc->ki_pid, &preExisting, (Process_New) FreeBSDProcess_new); + FreeBSDProcess* fp = (FreeBSDProcess*) proc; + + proc->show = ! ((hideKernelThreads && Process_isKernelThread(fp)) || (hideUserlandThreads && Process_isUserlandThread(proc))); + + if (!preExisting) { + fp->jid = kproc->ki_jid; + proc->pid = kproc->ki_pid; + if ( ! ((kproc->ki_pid == 0) || (kproc->ki_pid == 1) ) && kproc->ki_flag & P_SYSTEM) + fp->kernel = 1; + else + fp->kernel = 0; + proc->ppid = kproc->ki_ppid; + proc->tpgid = kproc->ki_tpgid; + proc->tgid = kproc->ki_pid; + proc->session = kproc->ki_sid; + proc->tty_nr = kproc->ki_tdev; + proc->pgrp = kproc->ki_pgid; + proc->st_uid = kproc->ki_uid; + proc->starttime_ctime = kproc->ki_start.tv_sec; + proc->user = UsersTable_getRef(this->usersTable, proc->st_uid); + ProcessList_add((ProcessList*)this, proc); + proc->comm = FreeBSDProcessList_readProcessName(fpl->kd, kproc, &proc->basenameOffset); + fp->jname = FreeBSDProcessList_readJailName(kproc); + } else { + if(fp->jid != kproc->ki_jid) { + // proces can enter jail anytime + fp->jid = kproc->ki_jid; + free(fp->jname); + fp->jname = FreeBSDProcessList_readJailName(kproc); + } + if (proc->ppid != kproc->ki_ppid) { + // if there are reapers in the system, proces can get reparented anytime + proc->ppid = kproc->ki_ppid; + } + if(proc->st_uid != kproc->ki_uid) { + // some proceses change users (eg. to lower privs) + proc->st_uid = kproc->ki_uid; + proc->user = UsersTable_getRef(this->usersTable, proc->st_uid); + } + if (settings->updateProcessNames) { + free(proc->comm); + proc->comm = FreeBSDProcessList_readProcessName(fpl->kd, kproc, &proc->basenameOffset); + } + } + + // from FreeBSD source /src/usr.bin/top/machine.c + proc->m_size = kproc->ki_size / 1024; + proc->m_resident = kproc->ki_rssize * pageSizeKb; + proc->nlwp = kproc->ki_numthreads; + proc->time = (kproc->ki_runtime + 5000) / 10000; + + proc->percent_cpu = 100.0 * ((double)kproc->ki_pctcpu / (double)kernelFScale); + if (proc->percent_cpu > 0.1) { + // system idle process should own all CPU time left regardless of CPU count + if ( strcmp("idle", kproc->ki_comm) == 0 ) { + isIdleProcess = true; + } else { + if (cpus > 1) + proc->percent_cpu = proc->percent_cpu / (double) cpus; + } + } + if (isIdleProcess == false && proc->percent_cpu >= 99.8) { + // don't break formatting + proc->percent_cpu = 99.8; + } + + proc->priority = kproc->ki_pri.pri_level - PZERO; + + if (strcmp("intr", kproc->ki_comm) == 0 && kproc->ki_flag & P_SYSTEM) { + proc->nice = 0; //@etosan: intr kernel process (not thread) has weird nice value + } else if (kproc->ki_pri.pri_class == PRI_TIMESHARE) { + proc->nice = kproc->ki_nice - NZERO; + } else if (PRI_IS_REALTIME(kproc->ki_pri.pri_class)) { + proc->nice = PRIO_MIN - 1 - (PRI_MAX_REALTIME - kproc->ki_pri.pri_level); + } else { + proc->nice = PRIO_MAX + 1 + kproc->ki_pri.pri_level - PRI_MIN_IDLE; + } + + switch (kproc->ki_stat) { + case SIDL: proc->state = 'I'; break; + case SRUN: proc->state = 'R'; break; + case SSLEEP: proc->state = 'S'; break; + case SSTOP: proc->state = 'T'; break; + case SZOMB: proc->state = 'Z'; break; + case SWAIT: proc->state = 'D'; break; + case SLOCK: proc->state = 'L'; break; + default: proc->state = '?'; + } + + if (Process_isKernelThread(fp)) { + this->kernelThreads++; + } + + this->totalTasks++; + if (proc->state == 'R') + this->runningTasks++; + proc->updated = true; + } +} diff --git a/freebsd/FreeBSDProcessList.h b/freebsd/FreeBSDProcessList.h new file mode 100644 index 0000000..2267379 --- /dev/null +++ b/freebsd/FreeBSDProcessList.h @@ -0,0 +1,69 @@ +/* Do not edit this file. It was automatically generated. */ + +#ifndef HEADER_FreeBSDProcessList +#define HEADER_FreeBSDProcessList +/* +htop - FreeBSDProcessList.h +(C) 2014 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + + +#include +#include +#include +#include +#include + +#define JAIL_ERRMSGLEN 1024 +char jail_errmsg[JAIL_ERRMSGLEN]; + +typedef struct CPUData_ { + + double userPercent; + double nicePercent; + double systemPercent; + double irqPercent; + double idlePercent; + double systemAllPercent; + +} CPUData; + +typedef struct FreeBSDProcessList_ { + ProcessList super; + kvm_t* kd; + + int zfsArcEnabled; + + unsigned long long int memWire; + unsigned long long int memActive; + unsigned long long int memInactive; + unsigned long long int memFree; + unsigned long long int memZfsArc; + + + CPUData* cpus; + + unsigned long *cp_time_o; + unsigned long *cp_time_n; + + unsigned long *cp_times_o; + unsigned long *cp_times_n; + +} FreeBSDProcessList; + + + + +ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId); + +void ProcessList_delete(ProcessList* this); + +char* FreeBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd); + +char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc); + +void ProcessList_goThroughEntries(ProcessList* this); + +#endif diff --git a/freebsd/Platform.c b/freebsd/Platform.c new file mode 100644 index 0000000..9e0c25b --- /dev/null +++ b/freebsd/Platform.c @@ -0,0 +1,206 @@ +/* +htop - freebsd/Platform.c +(C) 2014 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Platform.h" +#include "Meter.h" +#include "CPUMeter.h" +#include "MemoryMeter.h" +#include "SwapMeter.h" +#include "TasksMeter.h" +#include "LoadAverageMeter.h" +#include "UptimeMeter.h" +#include "ClockMeter.h" +#include "HostnameMeter.h" +#include "FreeBSDProcess.h" +#include "FreeBSDProcessList.h" + +#include +#include +#include +#include +#include +#include + +/*{ +#include "Action.h" +#include "BatteryMeter.h" +#include "SignalsPanel.h" + +extern ProcessFieldData Process_fields[]; + +}*/ + +#ifndef CLAMP +#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) +#endif + +ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; + +int Platform_numberOfFields = LAST_PROCESSFIELD; + +SignalItem Platform_signals[] = { + { .name = " 0 Cancel", .number = 0 }, + { .name = " 1 SIGHUP", .number = 1 }, + { .name = " 2 SIGINT", .number = 2 }, + { .name = " 3 SIGQUIT", .number = 3 }, + { .name = " 4 SIGILL", .number = 4 }, + { .name = " 5 SIGTRAP", .number = 5 }, + { .name = " 6 SIGABRT", .number = 6 }, + { .name = " 7 SIGEMT", .number = 7 }, + { .name = " 8 SIGFPE", .number = 8 }, + { .name = " 9 SIGKILL", .number = 9 }, + { .name = "10 SIGBUS", .number = 10 }, + { .name = "11 SIGSEGV", .number = 11 }, + { .name = "12 SIGSYS", .number = 12 }, + { .name = "13 SIGPIPE", .number = 13 }, + { .name = "14 SIGALRM", .number = 14 }, + { .name = "15 SIGTERM", .number = 15 }, + { .name = "16 SIGURG", .number = 16 }, + { .name = "17 SIGSTOP", .number = 17 }, + { .name = "18 SIGTSTP", .number = 18 }, + { .name = "19 SIGCONT", .number = 19 }, + { .name = "20 SIGCHLD", .number = 20 }, + { .name = "21 SIGTTIN", .number = 21 }, + { .name = "22 SIGTTOU", .number = 22 }, + { .name = "23 SIGIO", .number = 23 }, + { .name = "24 SIGXCPU", .number = 24 }, + { .name = "25 SIGXFSZ", .number = 25 }, + { .name = "26 SIGVTALRM", .number = 26 }, + { .name = "27 SIGPROF", .number = 27 }, + { .name = "28 SIGWINCH", .number = 28 }, + { .name = "29 SIGINFO", .number = 29 }, + { .name = "30 SIGUSR1", .number = 30 }, + { .name = "31 SIGUSR2", .number = 31 }, + { .name = "32 SIGTHR", .number = 32 }, + { .name = "33 SIGLIBRT", .number = 33 }, +}; + +unsigned int Platform_numberOfSignals = sizeof(Platform_signals)/sizeof(SignalItem); + +void Platform_setBindings(Htop_Action* keys) { + (void) keys; +} + +MeterClass* Platform_meterTypes[] = { + &CPUMeter_class, + &ClockMeter_class, + &LoadAverageMeter_class, + &LoadMeter_class, + &MemoryMeter_class, + &SwapMeter_class, + &TasksMeter_class, + &UptimeMeter_class, + &BatteryMeter_class, + &HostnameMeter_class, + &AllCPUsMeter_class, + &AllCPUs2Meter_class, + &LeftCPUsMeter_class, + &RightCPUsMeter_class, + &LeftCPUs2Meter_class, + &RightCPUs2Meter_class, + &BlankMeter_class, + NULL +}; + +int Platform_getUptime() { + struct timeval bootTime, currTime; + int mib[2] = { CTL_KERN, KERN_BOOTTIME }; + size_t size = sizeof(bootTime); + + int err = sysctl(mib, 2, &bootTime, &size, NULL, 0); + if (err) { + return -1; + } + gettimeofday(&currTime, NULL); + + return (int) difftime(currTime.tv_sec, bootTime.tv_sec); +} + +void Platform_getLoadAverage(double* one, double* five, double* fifteen) { + struct loadavg loadAverage; + int mib[2] = { CTL_VM, VM_LOADAVG }; + size_t size = sizeof(loadAverage); + + int err = sysctl(mib, 2, &loadAverage, &size, NULL, 0); + if (err) { + *one = 0; + *five = 0; + *fifteen = 0; + return; + } + *one = (double) loadAverage.ldavg[0] / loadAverage.fscale; + *five = (double) loadAverage.ldavg[1] / loadAverage.fscale; + *fifteen = (double) loadAverage.ldavg[2] / loadAverage.fscale; +} + +int Platform_getMaxPid() { + int maxPid; + size_t size = sizeof(maxPid); + int err = sysctlbyname("kern.pid_max", &maxPid, &size, NULL, 0); + if (err) { + return 99999; + } + return maxPid; +} + +double Platform_setCPUValues(Meter* this, int cpu) { + FreeBSDProcessList* fpl = (FreeBSDProcessList*) this->pl; + int cpus = this->pl->cpuCount; + CPUData* cpuData; + + if (cpus == 1) { + // single CPU box has everything in fpl->cpus[0] + cpuData = &(fpl->cpus[0]); + } else { + cpuData = &(fpl->cpus[cpu]); + } + + double percent; + double* v = this->values; + + v[CPU_METER_NICE] = cpuData->nicePercent; + v[CPU_METER_NORMAL] = cpuData->userPercent; + if (this->pl->settings->detailedCPUTime) { + v[CPU_METER_KERNEL] = cpuData->systemPercent; + v[CPU_METER_IRQ] = cpuData->irqPercent; + Meter_setItems(this, 4); + percent = v[0]+v[1]+v[2]+v[3]; + } else { + v[2] = cpuData->systemAllPercent; + Meter_setItems(this, 3); + percent = v[0]+v[1]+v[2]; + } + + percent = CLAMP(percent, 0.0, 100.0); + if (isnan(percent)) percent = 0.0; + return percent; +} + +void Platform_setMemoryValues(Meter* this) { + // TODO + ProcessList* pl = (ProcessList*) this->pl; + + this->total = pl->totalMem; + this->values[0] = pl->usedMem; + this->values[1] = pl->buffersMem; + this->values[2] = pl->cachedMem; +} + +void Platform_setSwapValues(Meter* this) { + ProcessList* pl = (ProcessList*) this->pl; + this->total = pl->totalSwap; + this->values[0] = pl->usedSwap; +} + +void Platform_setTasksValues(Meter* this) { + // TODO +} + +char* Platform_getProcessEnv(pid_t pid) { + // TODO + return NULL; +} diff --git a/freebsd/Platform.h b/freebsd/Platform.h new file mode 100644 index 0000000..ecc0dcd --- /dev/null +++ b/freebsd/Platform.h @@ -0,0 +1,51 @@ +/* Do not edit this file. It was automatically generated. */ + +#ifndef HEADER_Platform +#define HEADER_Platform +/* +htop - freebsd/Platform.h +(C) 2014 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Action.h" +#include "BatteryMeter.h" +#include "SignalsPanel.h" + +extern ProcessFieldData Process_fields[]; + + +#ifndef CLAMP +#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) +#endif + +extern ProcessField Platform_defaultFields[]; + +extern int Platform_numberOfFields; + +extern SignalItem Platform_signals[]; + +extern unsigned int Platform_numberOfSignals; + +void Platform_setBindings(Htop_Action* keys); + +extern MeterClass* Platform_meterTypes[]; + +int Platform_getUptime(); + +void Platform_getLoadAverage(double* one, double* five, double* fifteen); + +int Platform_getMaxPid(); + +double Platform_setCPUValues(Meter* this, int cpu); + +void Platform_setMemoryValues(Meter* this); + +void Platform_setSwapValues(Meter* this); + +void Platform_setTasksValues(Meter* this); + +char* Platform_getProcessEnv(pid_t pid); + +#endif -- cgit v1.2.3