18

I need to run some long and heavy commands, but at the same time, I'd like to keep my desktop system responsive.

Examples: btrfs deduplication, btrfs balance, etc. I don't mind if such commands take longer to finish if I give them a lower priority, but my system should always be responsive.

Using nice -n 19 and ionice -c 3 should solve my problem, but I'm not sure which command should come first for maximum benefit.

  • Option A:

    nice -n 19 ionice -c 3 btrfs balance start --full-balance /
    
  • Option B:

    ionice -c 3 nice -n 19 btrfs balance start --full-balance /
    

Is there some subtle difference between options A and B? Are they equivalent perhaps?

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • 3
    It would only make a difference if `ionice` were very computational, or `nice` did lots of I/O. Neither is true. – Barmar Sep 05 '17 at 17:21
  • @Barmar: If you create an answer based on your comment I'll accept it. Thank you. –  Sep 06 '17 at 09:56

2 Answers2

22

If nice caused lots of I/O, you would want to do:

ionice -c 3 nice ...

so that the impact of the I/O would be minimized.

Conversely, if ionice performed lots of computation, you would want to do

nice -n 19 ionice ...

to minimize its CPU impact.

But neither of these is true, they're both very simple commands (they just make a system call to change a process parameter, then execute the command). So the difference should be negligible.

And just to be complete, if both were true, you can't really win -- the impact of one of them can't be reduced.

Barmar
  • 9,648
  • 1
  • 19
  • 28
  • So what you are saying is in the case of `nice -n 19 ionice ...` nice modifies the priority of ionice; and in the case of `ionice -c 3 nice ...` ionice modifies the priority of nice. And in neither case do both commands modify the priorities of `btrfs balance ...`. I believe the desire is to have both commands affect the niceness of `btrfs balance ...`. – TiberiusKirk Dec 25 '20 at 00:49
  • 2
    @TiberiusKirk The priorities are inherited, so `btrfs balance` gets both priority adjustments. I was only addressing the difference between the order of the two adjustment commands. – Barmar Dec 25 '20 at 02:39
  • I just read somewhere that nice will also change the IO priority. Does that change this answer in any way? – Nabheet Jun 14 '21 at 22:35
  • @Nabheet I haven't found that in any of the online man pages. Can you find a reference that confirms that? – Barmar Jun 15 '21 at 00:53
  • The man page for ionice implies that the default io priority is `io_priority = (cpu_nice + 20) / 5`. https://unix.stackexchange.com/a/153636/134331. https://linux.die.net/man/1/ionice. I guess maybe `nice` doesn't change the IO priority, maybe the IO priority automatically changes based on CPU priority? – Nabheet Jun 15 '21 at 16:52
  • 1
    That's only the relative priority within the Best Effort I/O priority class. – Barmar Jun 15 '21 at 21:05
  • 1
    @Nabheet You still have to use `ionice` to select the I/O class. – Barmar Jun 15 '21 at 21:21
6

There is no practical difference between option A and option B.

sourcejedi
  • 48,311
  • 17
  • 143
  • 296