Blame SOURCES/sysstat-10.1.5-pids-prealloc.patch

ab3721
diff -upr sysstat-10.1.5.orig/pidstat.c sysstat-10.1.5/pidstat.c
ab3721
--- sysstat-10.1.5.orig/pidstat.c	2013-03-23 17:31:46.000000000 +0100
ab3721
+++ sysstat-10.1.5/pidstat.c	2016-04-29 13:04:02.672402150 +0200
ab3721
@@ -152,15 +152,35 @@ void salloc_pid_array(unsigned int len)
ab3721
  */
ab3721
 void salloc_pid(unsigned int len)
ab3721
 {
ab3721
-	int i;
ab3721
+	short i;
ab3721
+
ab3721
+	for (i = 0; i < 3; i++) {
ab3721
+		if ((st_pid_list[i] = (struct pid_stats *) calloc(len, PID_STATS_SIZE)) == NULL) {
ab3721
+			perror("calloc");
ab3721
+			exit(4);
ab3721
+		}
ab3721
+	}
ab3721
+}
ab3721
+
ab3721
+/*
ab3721
+ ***************************************************************************
ab3721
+ * Reallocate structures for PIDs to read.
ab3721
+ ***************************************************************************
ab3721
+ */
ab3721
+void realloc_pid(void)
ab3721
+{
ab3721
+	short i;
ab3721
+	unsigned int new_size = 2 * pid_nr;
ab3721
 
ab3721
 	for (i = 0; i < 3; i++) {
ab3721
-		if ((st_pid_list[i] = (struct pid_stats *) malloc(PID_STATS_SIZE * len)) == NULL) {
ab3721
-			perror("malloc");
ab3721
+		if ((st_pid_list[i] = (struct pid_stats *) realloc(st_pid_list[i], PID_STATS_SIZE * new_size)) == NULL) {
ab3721
+			perror("realloc");
ab3721
 			exit(4);
ab3721
 		}
ab3721
-		memset(st_pid_list[i], 0, PID_STATS_SIZE * len);
ab3721
+		memset(st_pid_list[i] + pid_nr, 0, PID_STATS_SIZE * (new_size - pid_nr));
ab3721
 	}
ab3721
+
ab3721
+	pid_nr = new_size;
ab3721
 }
ab3721
 
ab3721
 /*
ab3721
@@ -774,23 +794,22 @@ void read_task_stats(int curr, unsigned
ab3721
 	if ((dir = opendir(filename)) == NULL)
ab3721
 		return;
ab3721
 
ab3721
-	while (*index < pid_nr) {
ab3721
+	while ((drp = readdir(dir)) != NULL) {
ab3721
+		if (!isdigit(drp->d_name[0])) {
ab3721
+			continue;
ab3721
+		}
ab3721
 
ab3721
-		while ((drp = readdir(dir)) != NULL) {
ab3721
-			if (isdigit(drp->d_name[0]))
ab3721
-				break;
ab3721
+		pst = st_pid_list[curr] + (*index)++;
ab3721
+		if (read_pid_stats(atoi(drp->d_name), pst, &thr_nr, pid)) {
ab3721
+			/* Thread no longer exists */
ab3721
+			pst->pid = 0;
ab3721
 		}
ab3721
 
ab3721
-		if (drp) {
ab3721
-			pst = st_pid_list[curr] + (*index)++;
ab3721
-			if (read_pid_stats(atoi(drp->d_name), pst, &thr_nr, pid)) {
ab3721
-				/* Thread no longer exists */
ab3721
-				pst->pid = 0;
ab3721
-			}
ab3721
+		if (*index >= pid_nr) {
ab3721
+			realloc_pid();
ab3721
 		}
ab3721
-		else
ab3721
-			break;
ab3721
 	}
ab3721
+
ab3721
 	closedir(dir);
ab3721
 }
ab3721
 
ab3721
@@ -830,36 +849,34 @@ void read_stats(int curr)
ab3721
 			exit(4);
ab3721
 		}
ab3721
 
ab3721
-		while (p < pid_nr) {
ab3721
+		/* Get directory entries */
ab3721
+                while ((drp = readdir(dir)) != NULL) {
ab3721
+                        if (!isdigit(drp->d_name[0])) {
ab3721
+                                continue;
ab3721
+                        }
ab3721
+
ab3721
+                        pst = st_pid_list[curr] + p++;
ab3721
+                        pid = atoi(drp->d_name);
ab3721
+
ab3721
+                        if (read_pid_stats(pid, pst, &thr_nr, 0)) {
ab3721
+                            /* Process has terminated */
ab3721
+                            pst->pid = 0;
ab3721
+                        } else if (DISPLAY_TID(pidflag)) {
ab3721
+                            /* Read stats for threads in task subdirectory */
ab3721
+                            read_task_stats(curr, pid, &p);
ab3721
 
ab3721
-			/* Get directory entries */
ab3721
-			while ((drp = readdir(dir)) != NULL) {
ab3721
-				if (isdigit(drp->d_name[0]))
ab3721
-					break;
ab3721
-			}
ab3721
-			if (drp) {
ab3721
-				pst = st_pid_list[curr] + p++;
ab3721
-				pid = atoi(drp->d_name);
ab3721
-	
ab3721
-				if (read_pid_stats(pid, pst, &thr_nr, 0)) {
ab3721
-					/* Process has terminated */
ab3721
-					pst->pid = 0;
ab3721
-				}
ab3721
-	
ab3721
-				else if (DISPLAY_TID(pidflag)) {
ab3721
-					/* Read stats for threads in task subdirectory */
ab3721
-					read_task_stats(curr, pid, &p);
ab3721
-				}
ab3721
-			}
ab3721
-			else {
ab3721
-				for (q = p; q < pid_nr; q++) {
ab3721
-					pst = st_pid_list[curr] + q;
ab3721
-					pst->pid = 0;
ab3721
-				}
ab3721
-				break;
ab3721
 			}
ab3721
+
ab3721
+			if (p >= pid_nr) {
ab3721
+                            realloc_pid();
ab3721
+                        }
ab3721
 		}
ab3721
 
ab3721
+                for (q = p; q < pid_nr; q++) {
ab3721
+                    pst = st_pid_list[curr] + q;
ab3721
+                    pst->pid = 0;
ab3721
+                }
ab3721
+
ab3721
 		/* Close /proc directory */
ab3721
 		closedir(dir);
ab3721
 	}
ab3721
diff -upr sysstat-10.1.5.orig/pidstat.h sysstat-10.1.5/pidstat.h
ab3721
--- sysstat-10.1.5.orig/pidstat.h	2013-03-23 17:31:46.000000000 +0100
ab3721
+++ sysstat-10.1.5/pidstat.h	2016-04-28 18:24:37.700124018 +0200
ab3721
@@ -13,7 +13,7 @@
ab3721
 #define K_P_CHILD	"CHILD"
ab3721
 #define K_P_ALL		"ALL"
ab3721
 
ab3721
-#define NR_PID_PREALLOC	10
ab3721
+#define NR_PID_PREALLOC	100
ab3721
 
ab3721
 #define MAX_COMM_LEN	128
ab3721
 #define MAX_CMDLINE_LEN	128