Linux’ kscand

In Linux the kscand process is a kernel process which is a key component of the virtual memory (vm) system.  According to Unix Tech Tips & Tricks’ excellent Understanding Virtual Memory article “The kscand task periodically sweeps through all the pages in memory, taking note of the amount of time the page has been in memory since it was last accessed. If kscand finds that a page has been accessed since it last visited the page, it increments the page’s age counter; otherwise, it decrements that counter. If kscand finds a page with its age counter at zero, it moves the page to the inactive dirty state.”

For the majority of Linux users and even system administrators on large servers this kernel process requires no intervention.  It is a simple process that works in the background doing its job well.  Nonetheless, under certain circumstances it can become necessary to tune kscand in order to improve system performance in a desirable way.

Issues with kscand are most likely to arise in a situation where a Linux box has an extremely large amount of memory and will be even more noticeable on boxes with slower memory.  The most notable is probably the HP Proliant DL585 G1 which can support 128GB or memory but in doing so drops bandwidth to a paltry 266MHz.  I first came across this particular issue on a server with 32GB of memory with approximately 31.5GB of it in use.  No swap space was being used and most of the memory was being used for cache so there was no strain on the memory system but the total amount of memory being scanned by the kscand process is where the issue truly lies.

Even on a busy server with gobs of memory (that’s the technical term) it would be extremely rare that kscand would cause any issues.  It is a very light process that runs quite quickly.  You are most likely to see kscand as a culprit when investigating problems with latency sensitive applications on memory intensive servers.  The first time that I came across the need to tune kscand was while diagnosing a strange latency pattern of network traffic going to a high-performance messaging bus.  The latency was minor but small spikes were causing concern in the very sensitive environment.  kscand was spotted as the only questionable process receiving much system attention during the high latency periods.

Under normal conditions, that is default tuning, kscand will run every thirty seconds and will scan 100% of the system memory looking for memory pages that can be freed.  This sweep is quick but can easily cause measurable system latency if you look carefully.  Through carefull tuning we can reduce the latency caused by this process but we do so as a tradeoff with memory utilization efficiency.  If you have a box with significantly extra memory or extremely static memory, such as large cache sizes that change very slowly, you can safely tune away from memory efficiency towards low latency with nominal pain and good results.

kscand is controlled by the proc filesystem with just the single setting of  /proc/sys/vm/kscand_work_percent. Like any kernel setting this can be changed on the fly to a live system (be careful) or can be set to persist through reboots by adding it to your /etc/sysctl.conf file.  Before we make any permanent changes we will want to do some testing.  This kernel parameter tells kscand what percentage of the system memory to scan each time that a memory scan is performed.  Since it is normally set to 100 kscand normally scans all in-use memory each time that it is called.  You can verify you current setting quite easily.

cat /proc/sys/vm/kscand_work_percent

A good starting point with kscand_work_percent is to set to 50.  A very small adjustment may not be noticeable so seeing 100 and then 50 should provide a good starting point for evaluating the changes in system performance.  It is not recommended to set kscand_work_percent below 10 and I would be quite wary of dropping even below 20 unless you truly have a tremendous amount of unused memory and your usage is quite static.

echo 50 > /proc/sys/vm/kscand_work_percent

Once you have determined the best balance of latency and memory utilization that makes sense for your environment you can make you changes permanent.  Be sure to only use the echo technique if this is the first time that this will be added to the file. You will need to edit it by hand after that.

echo "kscand_work_percent = 50" >> /etc/sysctl.conf

Keep in mind that the need to edit this particular kernel parameter is extremely uncommon and will need to be done only under extraordinary circumstances.  You will not need to do this in normal, everyday Linux life and even a senior Linux administrator could easily never have need to modify this setting.  On very specific conditions will cause this performance characteristic to be measurable or its modification to be desirable.

All of my testing was done on Red Hat Enterprise Linux 3 Update 6.  This parameter is the same across many versions although the performance characteristics of kscand vary between kernel revisions so do not assume that the need to modify the parameters in one situation will mean that it is needed in another.

RHEL 3 prior to update 6 had a much less efficient kscand process and much greater benefit is likely to be found moving to a later 2.4 family kernel revision.  RHEL 4 and later, on the 2.6 series kernels, is completely different and the latency issues are, I believe, less pronounced.  In my own testing the same application on the same servers moving from RHEL 3 U6 to RHEL 4.5 removed all need for this tweak even under identical load.  [Edit – In RHEL 4 and later (kernel series 2.6) the kscand process has been removed and replaced with kswapd and pdflush.]

Things that are likely to impact the behavior of kscand that you should consider include the following:

  • Total Used Memory Size, regardless of total available memory size.  The more you have the more kscand will impact you.  Determined by: free -m | grep Mem | awk '{print $3}'
  • Memory Latency, check with your memory hardware vendor. Higher latency will cause kscand to have a larger impact.
  • Memory Bandwidth.  Currently in speeds ranging from 266MHz to 1066MHz.  The slower the memory the more likely a scan will impact you and tuning will be useful.
  • Value in kscand_work_percent. The lower the value the lower the latency.  The higher the value the better the memory utilization.
  • Memory Access Hops.  Number of system bus hops necessary to access memory resources.  For example a two socket AMD Opteron server (HP Proliant DL385) never has more than one hop.  But a four socket AMD Opteron server (HPProliant DL585) can have two hops increasing effective memory latency. So a DL585 is more likely to be affected than a DL385 with all other factors being equal (as long as all three or four processor sockets are occupied.)

Handbrake Settings

Now that I am getting comfortable with Handbrake and its settings I have noticed the dearth of information online about peoples’ preferred settings. I feel that this information is very valuable and so have decided to share some baseline information about my own settings.

Unless otherwise stated I do my conversions to the .mp4 MPEG4 container and extension using h.264 for the video and AAC for the audio. My intent is to have videos that play with excellent quality with consideration to storage requirements on my first generation AppleTV and on VLC. I am not concerned with compression time. I always use two pass compression and do not select “turbo first pass.”  I always use the “H.264 (x264)” codec option considering the target devices that I intend to use.

Update: I have since added a Sony Platstation 3 (PS3) to my output targets and am now using Handbrake 0.9.3 which is needed to support the PS3 due to codec limitations.  I am using this settings for AppleTV, PS3 and VLC for desktop use.

Anamorphic Cinema Content from 480p Source (such as DVD)

Bitrate: 2,500Kb/s
AAC Stereo: 160Kb/s – 48 & AC3 Pass-Through
Deinterlace: Slowest
Denoise: Weak
Deblock: On
Keep Aspect Ratio: On
Detelecine: VBR
Container: .m4v (Needed for AppleTV support of dual audio.)

Wide Screen HDTV Content from 480p Source

Bitrate: 2,200Kb/s
AAC Stereo: 160Kb/s – 48 & AC3 Pass-Through
Deinterlace: Slowest
Denoise: Weak
Deblock: On
Keep Aspect Ratio: On
Container: .m4v

Newer American Television NTSC (1990s Sitcoms)

Bitrate: 1,800Kb/s
AAC Stereo: 160Kb/s – 48
Deinterlace: Slowest
Denoise: Weak
Deblock: On

Traditional American Television (1980s Sitcoms)

Bitrate: 1,600Kb/s
AAC Stereo: 128Kb/s – 48
Deinterlace: Slowest
Denoise: Weak

Old British Television (1980s Britcoms)

Bitrate: 1,200Kb/s
AAC Stereo: 112Kb/s – 48
Deinterlace: Slow
Denoise: Weak

Cartoons

Bitrate: 400Kb/s
AAC Stereo: 112Kb/s – 48
Deinterlace: Slowest
Denoise: Weak
Deblock: Off
Decomb: On

Hopefully this collection of settings will provide you with a starting point in getting the most out of Handbrake. At this time I am currently using Handbrake 0.93 and getting great results from the x.264 encoder. For computer based playback I highly recommend the use of VLC over QuickTime as its playback is much smoother.

April 1, 2008: Working Late

How can it be April already?  That’s crazy.  March just flew right by.

I was really tired this morning and decided not to go in early like I usually try to do.  I like going in to the office early but once in a while I just need some time to sleep in like a normal person so that I can function all day long.

When I got to the office I discovered that I was locked out – which was to be expected as this is my first time back to the office since my two year anniversary rolled over over the weekend.  So I had to deal with that although it wasn’t too bad.  Now that I have done this before the process is much easier the second time around.  I also got myself access to the floors where a lot of my clients sit so I am not able to go meet people that I work for and near but have never actually seen.

Today was miraculously a slow day which I really needed after yesterday and the whole weekend.  After getting afternoon coffee I took advantage of my new found access card and checked out the big floor with my clients and spent a few hours just moving from team to team saying hello to people that I hadn’t seen in a while and meeting people that I have worked with for years but have never met.

Since I was out of the office for a while I got a little backed up.  Not too bad as the afternoon was slow, but there were some things that needed to be done and I ended up getting stuck in the office until after seven in the evening.  It was after eight by the time that I arrived home by way of the train.  Quite a long day and I was really tired.

Dominica was watching Family Guy when I got home.  I watched one episode and relaxed for twenty some minutes and then went to the living room and got started on my homework for the evening.  I have a bit to do this week and it is due on Friday night which means that there really isn’t any free time for me to get it done between now and then which will be, obviously, quite problematic.

I had the weirdest dream last night that Eric was offered one million dollars to be the spokesperson for Harry Anderson‘s political campaign.  I don’t remember if Eric took him up on the offer or not.  He ended up being offered a million dollar house to be on the campaign trail (for what I have no idea) and then one hundred thousand a year to be a political advisor.

March 31, 2008: Watching Bride and Prejudice

After a long weekend of work there was a lot of potential for issues this morning which, as were more or less expected, happened.  So I started on the early side working from the living room and ended up on phone calls and working on issues until well after lunch.  The day just flew by with so much work going on.  I barely noticed the day passing.  It was a gloomy, overcast day so there was no movement of the sun to mark the day’s progress either so I really didn’t notice.

It was almost two in the afternoon when I finally realized that I needed to get something to eat but didn’t have any time.  So I ordered from Airlie Cafe across the street and asked them to deliver a sandwich and salad over to me.  I am so glad that they deliver.  It is silly to have food delivered from right across the street but sometime there just isn’t any good way to leave the apartment.  They felt bad for me and delivered me some free garden vegetable soup too.

I worked a rather long day and by mid-afternoon was actually getting light headed.  I didn’t get a chance to do anything around the apartment today other than water the plants.  I was really exhausted by the end of the day.

Dominica got home and really wanted to watch Bride and Prejudice so we tried watching that but it was just one interruption after another including a page-out for me.  So I had to go work for about forty-five minutes in the middle of the movie, and Oreo needed constant attention.

Bride and Prejudice is a Miramax film and not from Bollywood and it shows.  If anything this could be called a mockery of Bollywood.  I don’t suggest wasting time on it.  It is an American view of Indian film’s twist on the English novel “Pride and Prejudice” and it does neither the book nor the culture any service.  The musical numbers are painful and the storyline is jilted.  The movie is bearable under some circumstances but it lacks the British charm of Bend It Like Beckham and is unquestionably made by Americans and not by Indians.  Most of the Indian scenes are even shot around London!

After the movie I stayed up for a little while doing some work and then off to bed.

March 30, 2008: Dominica Returns from Philadelphia

Oreo and I went to bed pretty late but we didn’t really sleep in this morning. It was around nine when Oreo got up and told me that there was sunlight to be had in the living room and that he wanted to partake of it. So I got up and he moved out into a nice, warm sun patch on a chair in the living room.

I was just starting to get settled in to actually do some more homework when I got paged from the office. Lot’s of issues this morning and tons of people are stuck working today because of it. I put in a couple of hours on a conference call. Things were busy enough that my cell phone kept ringing too. Not what I was expecting this morning. But, again, with Dominica not here I might as well be getting paged out rather than just sitting around doing nothing (although getting my homework caught up would be a nice change too.)

I did a tiny bit of cleaning early this afternoon and managed to get some laundry going. Normally Dominica does the laundry over the weekend but with her not here it could be a problem. I don’t like having her do the laundry but she gets mad if I try to do it because “I do it wrong” although I think that she is overcautious about colours and under-cautious about temperatures and over-drying. She thinks that I am colour-unsafe and wrinkle happy. So I only did my own laundry today so that she can only get so upset. Although she doesn’t like me doing even that.

I did do some other cleaning after Min called to say that she was on her way home from Philadelphia. She left around two in the afternoon. She had originally planned to come home earlier but Jenn and Angelo just had an offer accepted on a house and Jenn’s parents were down to see the house this morning so Dominica went with all of them so that they could all see it together.

I talked to dad today and right now the plan is for him to come down to Newark on April 19th and to stay until the 21st. We can’t do anything before then because of Dominica and my class schedules. My class, Object Technologies, at RIT is going to keep me very busy up until that weekend. I am hopeful that that weekend will be a “recovery” weekend after lots of other stuff comes due just days before. This is a rather busy semester for both of us.

It was nearly four when Dominica got back home. Oreo was so excited! We took a little break and got dinner from the deli downstairs and watched a few shoes on the AppleTV before returning to the living room to get back to work on our homework. 🙁

We both worked on homework all evening.  Then Dominica headed off to bed.  I didn’t wrap up homework until just after eleven at night!

Ryan, Kevin and I walked out to City Cafe and had a few drinks before we all turned in for the night.  We have all been so busy recently that none of us have seen each other at all.  We were only out for about an hour.  We were happy to discover that City Cafe stays open extremely late and even has their kitchen open at Thursday, Friday and Saturday nights until two in the morning!