Gentoo Archives: gentoo-amd64

From: Duncan <1i5t5.duncan@×××.net>
To: gentoo-amd64@l.g.o
Subject: [gentoo-amd64] Re: Managing CPU usage when doing emerges
Date: Mon, 18 May 2009 17:50:06
Message-Id: pan.2009.05.18.17.49.48@cox.net
In Reply to: Re: [gentoo-amd64] Managing CPU usage when doing emerges by Volker Armin Hemmann
1 Volker Armin Hemmann <volkerarmin@××××××××××.com> posted
2 200905181756.20052.volkerarmin@××××××××××.com, excerpted below, on Mon,
3 18 May 2009 17:56:19 +0200:
4
5 >> Wil Reichert wrote:
6 >> >
7 >> > I've got PORTAGE_NICENESS="15" set in my make.conf. Makes all
8 >> > emerges unnoticable on my system.
9 >
10 > 19 should be better. Once upon a time it marked portage as 'sched_batch'
11 > which was theoretically speeding up compiling.
12
13 Indeed. A nice of 19, the lowest priority possible, puts stuff in batch
14 mode. While it's lower priority and doesn't get as many time-slices to
15 work with, when it does get them, they are longer. This is perfect for
16 something that's not particularly interactive but IS quite CPU intensive,
17 as switching tasks at each timeslice is inefficient, so doing it less
18 frequently means more time is actually spent on-task. The tradeoff is
19 responsiveness, but for background/batch priority tasks, responsiveness
20 is assumed not to be an issue, so they can get the long timeslices to
21 increase efficiency with very little downside at all.
22
23 There's also ionice, which (depending on i/o scheduler) is normally
24 automatically set within the best-effort class based on normal/cpu nice.
25 However, setting ionice -c3 for a task puts it in idle priority class,
26 meaning it won't get disk i/o time unless no other program (not also of
27 idle class) has used the disk for a defined grace period. But to my
28 knowledge, ionice idle has no "batch benefit" as does normal nice.
29
30 But I didn't know until reading Nikos' post that there was a way to set
31 that with portage -- and have it apply to child processes as it would
32 need to do, as well. Thus I've not tried this and am simply copying it
33 here from his post, so everything's in one place:
34
35 PORTAGE_IONICE_COMMAND="ionice -c 3 -p \${PID}"
36
37 There's also something that can be done with the kernel (from 2.6.27
38 IIRC?? maybe .26 ??), that effectively gives each username its own CPU
39 quota (with root by default getting double the normal user default).
40 This means that no matter how a user prioritizes his tasks or what he's
41 trying to run, even a load of several hundred, if another user has stuff
42 to run, they get to use their quota and the user trying to hog simply
43 can't.
44
45 In menuconfig, general setup, ensure group CPU scheduler is enabled, then
46 enable scheduling for sched_other (at least, the RR/FIFO option is
47 special case, see its help for a pointer at the documentation), and if
48 need be, set the basis for grouping tasks to user_id, but that's the
49 default I think anyway.
50
51 (There's also control group scheduling which is somewhat different and
52 more complicated. It's for servers running multiple containers
53 instances, etc, so way beyond the level we're talking here. I'm
54 mentioning it just to say don't have them both on at the same time unless
55 you know what you are doing. If you're learning anything from this at
56 all, stick with the simple stuff and choose the user based scheduling.)
57
58 Once you have a kernel configured for that up and running, the knobs to
59 tweak it are in the /sys/kernel/uids/<user_ID>/cpu_share files, where
60 <user_ID> is the numeric user_ID for that user. The user_ID subdirs (and
61 their cpu_share files) will be dynamically created and erased as those
62 users are seen to be running anything by the kernel. Thus, all the
63 system daemons have files for whatever user they are run as, root has it
64 when anything is running as root, each human user has theirs when
65 anything is running as them, etc.
66
67 If you read the cpu_share files, you will see ordinary users are set to
68 1024 by default, while the root user (ID of 0) gets 2048 by default,
69 double the time of anyone else. This is a quite sane default setup, as
70 it means that you should always be able to login as root and kill a
71 process if it's hogging resources.
72
73 Now, if you are using portage (I assume the other PMs have something
74 similar) and are using FEATURES=userpriv, portage will spend most of its
75 high-CPU compiling time as the user "portage". FEATURES=userfetch and
76 usersync can be used as well, to have portage use its user for that too.
77 Of course, it must be root to run the install bit and when actually
78 qmerging to the live filesystem, but that's a very small part of its
79 normal work. With the kernel configured as described above, you now have
80 the kernel enforcing share so it can't hog things.
81
82 If you need, you can set your own user to 2048 or possibly 4096, 2-4
83 times the normal user share, the same or twice what root gets. That
84 should still give root enough time to do what it needs to do as long as
85 you only do that for a single user, but I'd be very cautious about doing
86 it for more than one user or setting > 4096.
87
88 Of course, you can also tighten down on hogging users, with one
89 limitation -- remember the user_id subdir goes away if there's nothing
90 left running as that user, and of course, over a reboot. Thus, you can't
91 set it as permanently as you might wish. There was a helper script that
92 was supposed to detect a userlogin and write a configured share to that
93 share file automatically, thus making it a basically permanent setting,
94 but I never could get the thing to work right here. The trigger that was
95 supposed to cause it to execute automatically never seemed to trigger, so
96 the script never ran.
97
98 Thus, it's often easier to adjust (presumably increasing) the share of
99 your normal human user when desired, than it is to adjust (presumably
100 decreasing, for portage) the share of a user that may be coming and
101 going, as the portage user would be, unless you're running an emerge
102 --emptytree @system @world (as I'm doing ATM, having just upgraded to gcc
103 4.4.0) or something.
104
105 With those, you should be able to set something sane for portage. Note
106 that if you have enough memory (I'd recommend 2 gigs at least better 4
107 gigs), you can point PORTAGE_TMPDIR at a tmpfs as well. That'll help
108 keep the I/O down while also /dramatically/ decreasing your merge times,
109 but you do need a decent amount of memory or it might make trying to work
110 with the system at the same time worse instead of better.
111
112 Using all of the above on a quad-core (or dual dual-cores, as mine), even
113 a portage load average of hundreds shouldn't be a problem, tho the memory
114 for it might be -- you don't want to go too far into swap. On a dual-
115 core (or dual-single-cores, as I ran for some time), you'll normally be
116 fine, but will notice a bit more jerkiness on the mouse and may have
117 issues with video or visualization, etc. With a single-core, you'll
118 still notice it to some extent, but it should still be usable, with
119 patience.
120
121 --
122 Duncan - List replies preferred. NoHTML msgs.
123 "Every nonfree program has a lord, a master --
124 and if you use the program, he is your master." Richard Stallman