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

ab3721
diff -upr sysstat-10.1.5.orig/common.h sysstat-10.1.5/common.h
ab3721
--- sysstat-10.1.5.orig/common.h	2013-03-23 17:31:46.000000000 +0100
ab3721
+++ sysstat-10.1.5/common.h	2016-04-29 12:47:09.285214132 +0200
ab3721
@@ -106,9 +106,12 @@
ab3721
  *
ab3721
  * NB: Define SP_VALUE() to normalize to %;
ab3721
  * HZ is 1024 on IA64 and % should be normalized to 100.
ab3721
+ * SP_VALUE_100() will not output value bigger than 100; this is needed for some
ab3721
+ *     corner cases, should be used with care.
ab3721
  */
ab3721
 #define S_VALUE(m,n,p)	(((double) ((n) - (m))) / (p) * HZ)
ab3721
 #define SP_VALUE(m,n,p)	(((double) ((n) - (m))) / (p) * 100)
ab3721
+#define SP_VALUE_100(m,n,p)	MINIMUM((((double) ((n) - (m))) / (p) * 100),100)
ab3721
 
ab3721
 /*
ab3721
  * Under very special circumstances, STDOUT may become unavailable.
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
@@ -1174,15 +1191,15 @@ int write_pid_task_all_stats(int prev, i
ab3721
 			printf(" %7.2f %7.2f %7.2f %7.2f",
ab3721
 			       (pstc->utime - pstc->gtime) < (pstp->utime - pstp->gtime) ?
ab3721
 			       0.0 :
ab3721
-			       SP_VALUE(pstp->utime - pstp->gtime,
ab3721
+			       SP_VALUE_100(pstp->utime - pstp->gtime,
ab3721
 					pstc->utime - pstc->gtime, itv),
ab3721
-			       SP_VALUE(pstp->stime,  pstc->stime, itv),
ab3721
-			       SP_VALUE(pstp->gtime,  pstc->gtime, itv),
ab3721
+			       SP_VALUE_100(pstp->stime,  pstc->stime, itv),
ab3721
+			       SP_VALUE_100(pstp->gtime,  pstc->gtime, itv),
ab3721
 			       /* User time already includes guest time */
ab3721
 			       IRIX_MODE_OFF(pidflag) ?
ab3721
-			       SP_VALUE(pstp->utime + pstp->stime,
ab3721
+			       SP_VALUE_100(pstp->utime + pstp->stime,
ab3721
 					pstc->utime + pstc->stime, g_itv) :
ab3721
-			       SP_VALUE(pstp->utime + pstp->stime,
ab3721
+			       SP_VALUE_100(pstp->utime + pstp->stime,
ab3721
 					pstc->utime + pstc->stime, itv));
ab3721
 
ab3721
 			printf("   %3d", pstc->processor);
ab3721
@@ -1351,15 +1368,15 @@ int write_pid_task_cpu_stats(int prev, i
ab3721
 		printf(" %7.2f %7.2f %7.2f %7.2f",
ab3721
 		       (pstc->utime - pstc->gtime) < (pstp->utime - pstp->gtime) ?
ab3721
 		       0.0 :
ab3721
-		       SP_VALUE(pstp->utime - pstp->gtime,
ab3721
+		       SP_VALUE_100(pstp->utime - pstp->gtime,
ab3721
 				pstc->utime - pstc->gtime, itv),
ab3721
-		       SP_VALUE(pstp->stime,  pstc->stime, itv),
ab3721
-		       SP_VALUE(pstp->gtime,  pstc->gtime, itv),
ab3721
+		       SP_VALUE_100(pstp->stime,  pstc->stime, itv),
ab3721
+		       SP_VALUE_100(pstp->gtime,  pstc->gtime, itv),
ab3721
 		       /* User time already includes guest time */
ab3721
 		       IRIX_MODE_OFF(pidflag) ?
ab3721
-		       SP_VALUE(pstp->utime + pstp->stime,
ab3721
+		       SP_VALUE_100(pstp->utime + pstp->stime,
ab3721
 				pstc->utime + pstc->stime, g_itv) :
ab3721
-		       SP_VALUE(pstp->utime + pstp->stime,
ab3721
+		       SP_VALUE_100(pstp->utime + pstp->stime,
ab3721
 				pstc->utime + pstc->stime, itv));
ab3721
 
ab3721
 		if (!disp_avg) {
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