You are not logged in.

#1 2013-05-26 18:11:33

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,601

[SOLVED]auto change disk scheduler on kernel switch

If this is not the appropriate place for this topic, feel free to move it around;)

In this topic started by flipper T , I asked about auto changing disk schedulers on kernel change
That is why I start this topic, as I have already forgotten to change the elevator once or twice, I like to prevent that from happening.
.
Now I was curious about the possibility to let there be a check when a kernel id chosen, to append a pre chosen scheduler, bfq, cfq, noop etc.
I thought of doing it with bash, but I think it's impossible cause there is no bash at that point, but am not sure.
WonderWoofy thought about using udev, which is a much better idea, cause it is started pretty early.
But I think udev is also not early enough.
I took a look at dmesg to see at what point the scheduler and udev were applied

 
[    0.733952] BFS CPU scheduler v0.430 by Con Kolivas.
[    0.740604] systemd-udevd[77]: starting version 204

In fact just before udev. So my question is, if I want this to work, in what direction must I go? EFI or a module?
Thanks

Last edited by qinohe (2013-06-02 23:13:33)

Offline

#2 2013-05-26 18:48:32

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: [SOLVED]auto change disk scheduler on kernel switch

Offline

#3 2013-05-26 18:54:20

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED]auto change disk scheduler on kernel switch

@brebs, so I brought this qeustion up in the thread qinohe linked to above.  I use a udev rule to apply a different scheduler to different types of media.  So I use something like this:

## set scheduler for non-rotating disks
ACTION=="add", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="deadline"

# set scheduler for rotating disks
ACTION=="add", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="cfq"

But I wanted to know if there was a way to have udev also poll for the booted kernel, so that I could have it select bfq if it happened to be booted from a CK kernel, and then use one of cfq, noop, or deadline othewise.

I think this is what qinohe is actually asking, and you are right that he has confused bfs with bfq.  What he means is bfq. 

@qinohe, this would explain why the scheduler was applied before udev started, because that message you are referring to in dmesg is actually the application of the cpu scheduler and not the I/O scheduler.

Offline

#4 2013-05-26 19:02:21

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: [SOLVED]auto change disk scheduler on kernel switch

WonderWoofy wrote:

to have udev also poll for the booted kernel, so that I could have it select bfq if it happened to be booted from a CK kernel, and then use one of cfq, noop, or deadline othewise.

Put it in a BASH startup script, which does a bit of uname checking.

Just have Udev call a script, rather than try to do it all with Udev's "ATTR" wink

Offline

#5 2013-05-26 19:07:14

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED]auto change disk scheduler on kernel switch

Yes this is true, and for all intents and purposes it would certainly work.  Though these days, I have started using "systemd --user" so I actually don't use the CK kernel anymore, as the bfs patch turns off a needed cgroup functionlaity in order to have the user sessions.

So for qinohe, I am sure that this would be a reasonable solution, and would work quite nicely.  But for me, it has become more of a curiosity as to whether there is some udev detectable difference in the /sys heirarchy between a booted CK kernel and a stock kernel.

Last edited by WonderWoofy (2013-05-26 19:14:41)

Offline

#6 2013-05-26 19:12:49

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: [SOLVED]auto change disk scheduler on kernel switch

You mean "intents and purposes" wink

/proc/sys/kernel/rr_interval only exists with the BFS scheduler.

Offline

#7 2013-05-26 19:17:29

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED]auto change disk scheduler on kernel switch

brebs wrote:

You mean "intents and purposes"

Yes, that is what I meant.  That is interesting with the /proc.  That I did not know.  Now is that something that is handlable by udev?  I was under the impression that udev polled for things under /sys only, and the /proc filesystem does its own thing.

Offline

#8 2013-05-26 19:53:55

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,601

Re: [SOLVED]auto change disk scheduler on kernel switch

I feel stupid, I think I know what I'm asking , just posted the wrong scheduler, yes, I mean bfq!

@WonderWoofy, yes that is exactly what I want.

But now my next question, how to implement it! Yesterday I wrote my first bash script, I don't even know if it works;)
Should I take WonderWoofy's way and use the udev rule, or do as brebs said, and use a bash script, which I will paste;

#!/bin/bash
if [uname -r = linux-ck];
then echo bfq > /sys/block/sda/queue/scheduler
fi
else
if [uname -r = linux];
then echo cfq > /sys/block/sda/queue/scheduler
fi

He, now don't judge me to hard;)

Offline

#9 2013-05-26 19:59:07

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED]auto change disk scheduler on kernel switch

qinohe wrote:

Should I take WonderWoofy's way and use the udev rule, or do as brebs said, and use a bash script

I would do a combination of both if you are looking to have different I/O schedulers for different devices.  But if it is simply the desire to set bfq globally for the CK kernel or a different scheduler globally for other kernels, then I think this can all be diregarded in favor of a kernel command line option.  That is, with the CK kernel you woulduse elevator=bfq, while with the others you would use elevator=deadline|noop. If cfq is desired, then no setting is required, as that is the default.  In fact, I think that with the CK kernel, bfq is actually the default there (at least the one built by graysky), so maybe no command line arg is required there either.

BTW, you should probably make the "uname -r" a variable, the use that in the test.  Having a plain "uname -r" is not going to actually do anything,  It is giong to try to equate "uname -r" as an actual string.  It won't run the command.  You could use "$(uname -r) in the test to make it evaluate the result of the uname -r command as well.

Last edited by WonderWoofy (2013-05-26 20:02:02)

Offline

#10 2013-05-26 20:26:03

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,601

Re: [SOLVED]auto change disk scheduler on kernel switch

WonderWoofy wrote:
qinohe wrote:

Should I take WonderWoofy's way and use the udev rule, or do as brebs said, and use a bash script

I would do a combination of both if you are looking to have different I/O schedulers for different devices.

It's only for the SSD, the HD is static cfq 

But if it is simply the desire to set bfq globally for the CK kernel or a different scheduler globally for other kernels, then I think this can all be diregarded in favor of a kernel command line option.  That is, with the CK kernel you woulduse elevator=bfq, while with the others you would use elevator=deadline|noop. If cfq is desired, then no setting is required, as that is the default.  In fact, I think that with the CK kernel, bfq is actually the default there (at least the one built by graysky), so maybe no command line arg is required there either.

Exact , I just want the change to happen automagicly at kernel change, but bfq needs interaction, as it is dormant by default, that says the wiki!

BTW, you should probably make the "uname -r" a variable, the use that in the test.  Having a plain "uname -r" is not going to actually do anything,  It is giong to try to equate "uname -r" as an actual string.  It won't run the command.  You could use "$(uname -r) in the test to make it evaluate the result of the uname -r command as well.

I get back on this, my bash 'foo' is not good , bad even as you see, gona try to sort this out in the days to come, I really need to give myself some more scripting experience, as a present wink

Offline

#11 2013-05-26 20:40:46

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED]auto change disk scheduler on kernel switch

If it is only for the SSD, then I would use a udev rule to match a non-rotational device with a kernel name of /dev/sd[a-z] and then have it RUN+="/your/script/here"

So a combination of the two.

Last edited by WonderWoofy (2013-05-26 20:41:02)

Offline

#12 2013-05-26 21:05:07

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,601

Re: [SOLVED]auto change disk scheduler on kernel switch

So this is not possible?
I had this rules, just changed the kernel, and added an extra line!

# set bfq scheduler for non-rotating disks with linux ck-kernel
ACTION=="add|change", KERNEL=vmlinuz-linux-ck="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="bfq"

# set cfq scheduler for non-rotating disks with linux-kernel
ACTION=="add|change", KERNEL=vmlinuz-linux="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="cfq"

# set cfq scheduler for rotating disk
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="cfq"

Offline

#13 2013-05-26 22:05:50

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED]auto change disk scheduler on kernel switch

I don't think that this is how the "KERNEL=" option is meant to be used.  Are you saying you ahve tried this?

Offline

#14 2013-05-27 05:51:46

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: [SOLVED]auto change disk scheduler on kernel switch

I suggest:

ACTION=="add|change", KERNEL=="sd[a-z]", RUN+="/root/bin/iodrive %k"

Then do the fancy work in that iodrive script. Don't forget to chmod 700 it.

Edit: Added %k, to pass the drive to the script.

Last edited by brebs (2013-05-27 07:07:58)

Offline

#15 2013-05-27 08:55:13

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,601

Re: [SOLVED]auto change disk scheduler on kernel switch

WonderWoofy wrote:

I don't think that this is how the "KERNEL=" option is meant to be used.  Are you saying you ahve tried this?

He, after reading some of this guide, I see! And no no I didn't have the guts to try!

brebs wrote:

I suggest:
ACTION=="add|change", KERNEL=="sd[a-z]", RUN+="/root/bin/iodrive %k"

Then do the fancy work in that iodrive script. Don't forget to chmod 700 it.

Edit: Added %k, to pass the drive to the script.

Ha thanks, like you suggested before, not to try and use 'ATTR' to do that.

I have a look later on today!

Offline

#16 2013-05-28 12:26:18

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,601

Re: [SOLVED]auto change disk scheduler on kernel switch

So, I have done some reading about this, and came up with the next little script.
Maybe it can be made smaller, but lets get this to work first;)

#!/bin/bash
if [[ "$(uname -r) | grep *-ARCH" ]] ; then 
var1="echo cfq > /sys/block/sda/queue/scheduler"
echo "$var1"
fi
if [[ "$(uname -r) | grep  *-ck" ]] ; then 
var2="echo bfq > /sys/block/sda/queue/scheduler"
echo "$var2"
fi

I ran

bash -x SSD

which gave the following output, does it look OK?

++ uname -r
+ [[ -n 3.9.4-1-ARCH | grep *-ARCH ]]
+ var1='echo cfq > /sys/block/sda/queue/scheduler'
+ echo 'echo cfq > /sys/block/sda/queue/scheduler'
echo cfq > /sys/block/sda/queue/scheduler
++ uname -r
+ [[ -n 3.9.4-1-ARCH | grep  *-ck ]]
+ var2='echo bfq > /sys/block/sda/queue/scheduler'
+ echo 'echo bfq > /sys/block/sda/queue/scheduler'
echo bfq > /sys/block/sda/queue/scheduler

Offline

#17 2013-05-28 14:32:22

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED]auto change disk scheduler on kernel switch

Your use of the test (ie. "[" and "]") is a bit wrong here.  The first one, you are not really testing it against any conditions, and the second you would need to be putting the stuff you are testing in backticks or in a $(command) so that it is run before the test takes place.  I don't want to just write a script for you, as it would take away the learning process, but start with this.  Make the thing you are trying to test (the kernel version) the variable.  So in this case, it might be something like this:

_kernel=$(uname -r | cut -d'-' -f3)

With the CK kernel, this would result in "CK" and with the official kernel it would be "ARCH".  So you would then test this quoted variable against those things:

[[ "$_kernel" = "CK" ]]; then ... 

Start with that knowledge and work your way through it.  I assume you can look through some other scripts and figure out an if/else statement pretty easily.  Just know that the test command will throw a 0 if it is equal and a 1 if it is not.  That exit code determines whether or not it will proceed with the "then" or not.

Edit: There is also probably a better way to test for the kernel version, as the "cut" part depends on the version string being the same exact format every time.  So it is using the "-" as the delimiter and taking the third part of split up by that delimit assumption.  So you are assuming there is going to be a version number, a pkgrel, and then the appending of descriptive thing at the end (I'm not sure what you would call that).

Last edited by WonderWoofy (2013-05-28 14:34:55)

Offline

#18 2013-05-28 15:36:15

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,601

Re: [SOLVED]auto change disk scheduler on kernel switch

I'm glad you don't hand the answer;), for sure I like to really understand this.
But reading through your answer only made it more complicated, which always seems to happen when I learn something new.
Now still I'm going to sort this, maybe not today or this week, but I will!
If you would happen to have some advice on a good guide/book, there  hundreds / thousands of them, difficult to say what is really a good one!

Offline

#19 2013-05-28 16:37:59

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED]auto change disk scheduler on kernel switch

I didn't really learn the little bit of scripting I know from any book.  I mostly just read through scripts when I wanted to modify or change some behavior, then went on the internets and googled the parts I didn't understand.  I have to agree with ewaller that a good way to learn how to code/script is to read good code or script.  It may not make total sense at first, but try to pick up on what you know and use that as a jumping point to delve deeper into the parts you don't know.  I think a great place to start looking through some well written bash scripts are on your own machine.  The Arch Linux devs do some pretty amazing scripting, and everyone has mkinitpcio on their machine.  There is also netctl and/org netcfg, and if you have netcfg, then there is openresolv on your system, as well as dhcpcd.

Start learning my friend!

Offline

#20 2013-05-28 16:59:29

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [SOLVED]auto change disk scheduler on kernel switch

FYI: To do a test based on the outcome of a command, you simply add if before it:

if uname -r | grep -q *-ARCH"
...

You need to make sure to hide the output though, as I did by adding the -q flag to grep.

That said, WonderWoofy is right about using a variable for your tests here. For the variable, I suggest storing the full 'uname -r' string, then using bash paramater expansion to parse it down:

_kernel=$(uname -r)
...
if [[ ${_kernel##*-} = ARCH ]]

Last edited by alphaniner (2013-05-28 16:59:51)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#21 2013-05-28 17:36:02

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED]auto change disk scheduler on kernel switch

Why not just set the default scheduler with elevator=bfq or elevator=cfq in the kernel parameters?

Offline

#22 2013-05-28 22:06:04

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED]auto change disk scheduler on kernel switch

falconindy wrote:

Why not just set the default scheduler with elevator=bfq or elevator=cfq in the kernel parameters?

I suggested this earlier.  But I this is mainly for the idea that you might want a scheduler 'A' for a SSD, and scheduler 'B' for a rotational with the Arch kernel, but scheduler 'C' if a rotational with the CK kernel (since it offers an additional elevator option).  Or at least something to that effect.  I agree if you want bfq across all drives then setting it on the kernel command line is hte way to go.  For me this has just become a hypothetical.

Offline

#23 2013-05-30 17:37:32

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,601

Re: [SOLVED]auto change disk scheduler on kernel switch

WonderWoofy wrote:

I didn't really learn the little bit of scripting I know from any book.  I mostly just read through scripts when I wanted to modify or change some behavior, then went on the internets and googled the parts I didn't understand.  I have to agree with ewaller that a good way to learn how to code/script is to read good code or script.  It may not make total sense at first, but try to pick up on what you know and use that as a jumping point to delve deeper into the parts you don't know.  I think a great place to start looking through some well written bash scripts are on your own machine.  The Arch Linux devs do some pretty amazing scripting, and everyone has mkinitpcio on their machine.  There is also netctl and/org netcfg, and if you have netcfg, then there is openresolv on your system, as well as dhcpcd.

Start learning my friend!

Hehe, yes I start again! But I needed to really bend my head around it, and still not all the pieces fell into place, but that will probably take time, and a lot of practice.
Maybe the part about learning from a good script could be true, but I really needed to have some basic info about bash,  glue that to the fact that I'm not native English speaker, well you get a nice soup!, bashsoup I guess;) WellI didi what you suggested, see later on!

@alphaniner, it just might have helped me a lot, we'll see later!
Thanks for that info page, I already read a lot of it!


@falconindy, yes the basic idee behind this is,

  • the rotational drive will always have cfq as scheduler.

  • The non-rotational disk will change to, bfq=CK and to cfq=ARCH

  • The change is done by the next script, hoppefully!)

Well, this is what I have so far;

#!/bin/bash
_kernel=$(uname -r | cut -d'-' -f3)
	if [ "$_kernel"="ARCH" ]; then 
echo "cfq > /sys/block/sda/queue/scheduler"; fi
	if [ "$_kernel"="CK" ]; then 
echo "bfq > /sys/block/sda/queue/scheduler"; fi

Am I working in the right direction?

Offline

#24 2013-05-30 18:03:51

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: [SOLVED]auto change disk scheduler on kernel switch

Instead of sda you should use $1 (i.e. the first parameter), which will be the %k in the RUN+= string in the udev rule I posted above.

Offline

#25 2013-05-30 21:41:15

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED]auto change disk scheduler on kernel switch

I am not sure if this is necessary... but in every script i have seen, there is whitespace between the parameters in the test brackets.  So it would be [ "$_kernel" = "CK" ].  Maybe it is just for readability, but this is what I have always seen. 

Brebs is right about using a variable in place of sda.  You may have only one drive, but if you had more then there is the chance that it might not end up being sda.  So "$1" is the first parameter, which means that if you had a command with an argument, (say $ echo "hello" ) then that $1 would represent the first argument (in the example it would be "hello").  If you had three arguments to the command, then they could be represented as $1, $2, and $3.

Oh and the echo lines are misquoted.  In that case it would actually print "bfq > /sys/block/sda/queue/scheduler".  So you want the echo "bfq" to be redirected (>) into that path.  Having the double quotes around that whole thing would group bfq with the path, which is not what you are trying to do.  It is a good idea to get in the habit of quoting things, but in this case it isn't actually necesary since you are echoing a string with no white space.  If you wanted it to print "hello world" then it would then be necessay to do echo "hello world" so that the command knows that those two words are grouped together as one argument.

Offline

Board footer

Powered by FluxBB