Skip to contentSkip to conversation

getrusage: return ru_maxrss

PATCH In review Frank Mayhar 1 participant 2 messages Aug 10, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 getrusage: return ru_maxrss

Fill in ru_maxrss by, for RUSAGE_BOTH or RUSAGE_CHILDREN, walking the list of children and summing the hiwater_rss for each. For RUSAGE_BOTH or RUSAGE_SELF also include the hiwater_rss for the calling process.

kernel/sys.c +18 -0
index 449b81b..71b2850 100644
@@ -2101,6 +2101,19 @@ static void k_getrusage(struct task_stru
 			r->ru_inblock = p->signal->cinblock;
 			r->ru_oublock = p->signal->coublock;
 
+			read_lock(&tasklist_lock);
+			list_for_each(_p, &p->children) {
+				struct task_struct *mp;
+
+				mp = list_entry(_p, struct task_struct, sibling);
+				mm = get_task_mm(mp);
+				hiwater_rss = get_mm_rss(mm);
+				if (hiwater_rss < mm->hiwater_rss)
+					hiwater_rss = mm->hiwater_rss;
+				r->ru_maxrss += hiwater_rss;
+			}
+			read_unlock(&tasklist_lock);
+
 			if (who == RUSAGE_CHILDREN)
 				break;
 
@@ -2125,6 +2138,11 @@ static void k_getrusage(struct task_stru
 				r->ru_oublock += task_io_get_oublock(t);
 				t = next_thread(t);
 			} while (t != p);
+			mm = get_task_mm(p);
+			hiwater_rss = get_mm_rss(mm);
+			if (hiwater_rss < mm->hiwater_rss)
+				hiwater_rss = mm->hiwater_rss;
+			r->ru_maxrss += hiwater_rss;
 			break;
 
 		default:

---

Like a number of other folks, we recently had a need for a non-/proc way of getting the RSS of a process and happened on getrusage(). Unlike the rest, though, we fixed the system call to do what we want.

The code itself was lifted from task_mem() in proc/task_mmu.c. The only questionable bit is where it does the read_lock() of the tasklist_lock; although it seems safe enough, I would appreciate if those more knowledgeable about that locking vet it (briefly) for safety.

Signature · 2 lines
Frank Mayhar <[email protected]>
Google, Inc.
#1 lore raw

Re: [PATCH 2.6.23-rc2] getrusage: return ru_maxrss *WITHDRAWN*

For some reason, the first twenty times I read the doc, I missed the "terminated and waited-for" bit for RUSAGE_CHILDREN.

Patch withdrawn, for the moment anyway. (And the right fix won't solve our problem, sigh.)

Signature · 2 lines
Frank Mayhar <[email protected]>
Google, Inc.