0

I use python to write a simple python program to set quota for a thread.

# make path
path = "/sys/fs/cgroup/cpu/%d" % (tid)
os.mkdir(path) 

# to set
set_quota = "echo %d > /sys/fs/cgroup/cpu/%d/cpu.cfs_quota_us" % (quota_us, tid)
os.system(set_quota)
set_quota = "echo %d > /sys/fs/cgroup/cpu/%d/tasks" % (tid, tid)
os.system(set_quota)

# to close
set_quota = "echo %d > /sys/fs/cgroup/cpu/%d/cpu.cfs_quota_us" % (-1, tid)
os.system(set_quota)
set_quota = "echo %d > /sys/fs/cgroup/cpu/%d/tasks" % (tid, tid)
os.system(set_quota)

I am sure that the tid exists but it appears when I start run this program so I need to make a path for it explicitly. But I get an error about sh: echo: I/O error. Why is it and how to solve it?

Note: the I/O error occurs after echo > /sys/fs/cgroup/cpu//cpu.cfs_quota_us

skytree
  • 1,060
  • 2
  • 13
  • 38
  • Do you have permissions needed to perform that operation? – Stephen Rauch Feb 24 '18 at 22:18
  • @Stephen Rauch Yes, I use `sudo su` and is root. – skytree Feb 24 '18 at 22:22
  • Why use `os.system()` here? You could directly open the files and write to them with native Python with *far* less overhead. Not that that would fix your issues here, which come from the kernel not liking what you're writing. – Charles Duffy Feb 24 '18 at 22:25
  • 2
    `f = open('/sys/fs/cgroup/cpu/%d/tasks' % tid, 'w'); f.write(str(tid) + '\n'); f.close()` – Charles Duffy Feb 24 '18 at 22:25
  • the fact that you have set and unset hints you to take a look at context manager, specifically, https://docs.python.org/2/library/contextlib.html – Jason Hu Feb 24 '18 at 22:27
  • @Charles Duffy Firstly, the path is made according to program that I don't know the tid before it runs. So I need to make a path for it. I think I cannot use open here(Can I open it before I create it or will it create it for me?); Secondly, how much difference between `"echo %d > /sys/fs/cgroup/cpu/%d/cpu.cfs_quota_us" % (-1, tid)` and `f.write(str(tid) + '\n')`? Does the overhead incur this I/O problem? – skytree Feb 24 '18 at 22:29
  • 1
    @skytree, the "I/O problem" is the kernel rejecting what you're writing to it. That's based on the content of what you `echo`, not the code you use to echo it, so switching from shell to Python won't change it at all. – Charles Duffy Feb 24 '18 at 22:31
  • 2
    @skytree, ...if I were asserting that changing to Python would *solve the problem*, I'd be writing an answer, not a comment. The comment is just pointing out that using `echo` is silly; it serves no purpose whatsoever except to make your program slower (and potentially vulnerable to shell-related bugs). – Charles Duffy Feb 24 '18 at 22:32
  • 1
    @skytree, ...and yes, I know you're generating the path at runtime -- that's why the code in my comment above uses string substitution to generate the argument it passes to `open()`. – Charles Duffy Feb 24 '18 at 22:34
  • @Charles Duffy Great insight, and let me try and give you feedback later. Thanks. – skytree Feb 24 '18 at 22:37
  • @HuStmpHrrr Sorry, I read your link and cannot get the point... – skytree Feb 24 '18 at 22:38
  • Add a print statement before every `os.system` call, i.e. `print(set_quota)`. That might help you figure out what's going on by showing you exactly what you're passing to `os.system`. – dogoncouch Feb 24 '18 at 23:15
  • @Charles Duffy Great! It works. Besides, I think another problem is because "The minimum quota allowed for the quota or period is 1ms." [link](https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt). Sometimes I set it too low. – skytree Feb 24 '18 at 23:53

0 Answers0