@cryptotaxi247 / netdata-1 / commits / 5aabeec37

eliminate a constant call to reallocz and make sure there is enough space for new processes

Costa Tsaousis (ktsaou) committed Jan 13, 2018 at 02:50 UTC 5aabeec377b0c16d59a00a805a18c5897f6ecc5c
1 file changed +28 -10
src/apps_plugin.c
+28 -10
@@ -2069,24 +2069,42 @@ static int collect_data_for_all_processes(void) {
2069
2070 #ifdef __FreeBSD__
2071 int i, procnum;
2072 - size_t procbase_size;
2073 - static struct kinfo_proc *procbase;
2072
2075 - int mib[3];
2073 + static size_t procbase_size = 0;
2074 + static struct kinfo_proc *procbase = NULL;
2075
2077 - mib[0] = CTL_KERN;
2078 - mib[1] = KERN_PROC;
2079 - mib[2] = KERN_PROC_PROC;
2080 - if (unlikely(sysctl(mib, 3, NULL, &procbase_size, NULL, 0))) {
2076 + size_t new_procbase_size;
2077 +
2078 + int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_PROC };
2079 + if (unlikely(sysctl(mib, 3, NULL, &new_procbase_size, NULL, 0))) {
2080 error("sysctl error: Can't get processes data size");
2081 return 0;
2082 }
2084 - procbase = reallocz(procbase, procbase_size);
2085 - if (unlikely(sysctl(mib, 3, procbase, &procbase_size, NULL, 0))) {
2083 +
2084 + // give it some air for processes that may be started
2085 + // during this little time.
2086 + new_procbase_size += 100 * sizeof(struct kinfo_proc);
2087 +
2088 + // increase the buffer if needed
2089 + if(new_procbase_size > procbase_size) {
2090 + procbase_size = new_procbase_size;
2091 + procbase = reallocz(procbase, procbase_size);
2092 + }
2093 +
2094 + // sysctl() gets from new_procbase_size the buffer size
2095 + // and also returns to it the amount of data filled in
2096 + new_procbase_size = procbase_size;
2097 +
2098 + // get the processes from the system
2099 + if (unlikely(sysctl(mib, 3, procbase, &new_procbase_size, NULL, 0))) {
2100 error("sysctl error: Can't get processes data");
2101 return 0;
2102 }
2089 - procnum = procbase_size / sizeof(struct kinfo_proc);
2103 +
2104 + // based on the amount of data filled in
2105 + // calculate the number of processes we got
2106 + procnum = new_procbase_size / sizeof(struct kinfo_proc);
2107 +
2108 #endif
2109
2110 if(all_pids_count) {