Microsoft, Page 2

User Mode Scheduling in Windows 7

Don’t use threads. Or more pre­cisely, don’t over-use them. It’s one of the first thing fledg­ling pro­gram­mers learn after they start using threads. This is be­cause thread­ing in­volves a lot of over­head. In short, using more threads may im­prove con­cur­rency, but it will give you less over­all through­put as more pro­cess­ing is put into sim­ply man­ag­ing the threads in­stead of let­ting them run. So pro­gram­mers learn to use threads spar­ingly.

When nor­mal threads run out of time, or block on some­thing like a mutex or I/O, they hand off con­trol to the op­er­at­ing sys­tem ker­nel. The ker­nel then finds a new thread to run, and switches back to user-mode to run the thread. This con­text switch­ing is what User Mode Sched­ul­ing looks to al­le­vi­ate.

User Mode Sched­ul­ing can be thought of as a cross be­tween threads and thread pools. An ap­pli­ca­tion cre­ates one or more UMS sched­uler threads—typ­i­cally one for each proces­sor. It then cre­ates sev­eral UMS worker threads for each sched­uler thread. The worker threads are the ones that run your ac­tual code. When­ever a worker thread runs out of time, it is put on the end of its sched­uler thread’s queue. If a worker thread blocks, it is put on a wait­ing list to be re-queued by the ker­nel when what­ever it was wait­ing on fin­ishes. The sched­uler thread then takes the worker thread from the top of the queue and starts run­ning it. Like the name sug­gests, this hap­pens en­tirely in user-mode, avoid­ing the ex­pen­sive user->ker­nel->user-mode tran­si­tions. Let­ting each thread run for ex­actly as long as it needs helps to solve the through­put prob­lem. Work is only put into man­ag­ing threads when ab­solutely nec­es­sary in­stead of in ever smaller time slices, leav­ing more time to run your ac­tual code.

A good side ef­fect of this is UMS threads also help to al­le­vi­ate the cache thrash­ing prob­lems typ­i­cal in heav­ily-threaded ap­pli­ca­tions. For­get­ting your data shar­ing pat­terns, each thread still needs its own stor­age for stack space, proces­sor con­text, and thread-lo­cal stor­age. Every time a con­text switch hap­pens, some data may need to be pushed out of caches in order to load some ker­nel-mode code and the next thread’s data. By switch­ing be­tween threads less often, cache can be put to bet­ter use for the task at hand.

If you have ever had a chance to use some of the more es­o­teric APIs in­cluded with Win­dows, you might be won­der­ing why we need UMS threads when we have fibers which offer sim­i­lar co-op­er­a­tive mul­ti­task­ing. Fibers have a lot of spe­cial ex­cep­tions. There are things that aren’t safe to do with them. Li­braries that rely on thread-lo­cal stor­age, for in­stance, will likely walk all over them­selves if used from within fibers. A UMS thread on the other hand is a full fledged thread—they sup­port TLS and no have no real spe­cial things to keep in mind while using them.

I still wouldn’t count out thread pools just yet. UMS threads are still more ex­pen­sive than a thread pool and the large mem­ory re­quire­ments of a thread still apply here, so things like per-client threads in in­ter­net dae­mons are still out of the ques­tion if you want to be mas­sively scal­able. More likely, UMS threads will be most use­ful for build­ing thread pools. Most thread pools launch two or three threads per CPU to help stay busy when given block­ing tasks, and UMS threads will at least help keep their time slice usage op­ti­mal.

From what I un­der­stand the team be­hind Mi­crosoft’s Con­cur­rency Run­time, to be in­cluded with Vi­sual C++ 2010, was one of the pri­mary forces be­hind UMS threads. They worked very closely with the ker­nel folks to find the most scal­able way to en­able the su­per-par­al­lel code that will be pos­si­ble with the CR.

My Windows Vista/7/8 Wishlist

These are some changes I’ve been try­ing to get made since Vista en­tered beta. Now 7’s beta has begun and still chances look bleak. Maybe I’ll have more luck in 8?

Windows 7 Beta will be free to the public

Not part of the one of the Win­dows 7 beta teams? On Jan­u­ary 9th, the first 2.5 mil­lion peo­ple to visit the Win­dows 7 home­page will be able to down­load the beta for free.

I just got my copy in­stalled a few hours ago, so far I’ve seen a few new fea­tures I like and cou­ple that I’m not sure about. I will blog about specifics as soon as I’m cer­tain what I’m al­lowed to men­tion.

And so, the Windows 7 tech beta begins.

Got my in­vite to the Win­dows 7 tech beta today. The first beta won’t be out until early 2009, but from what I’ve heard the cur­rent in­ter­nal copies of Win­dows 7 are al­ready a pretty good im­prove­ment over Vista, in both per­for­mance and us­abil­ity. Look­ing for­ward to work­ing with all the fine folks I’ve met from the last few betas!

C#: lamer by the moment

To my dis­be­lief, you can’t in­herit from generic type pa­ra­me­ters in C#. Some­thing like this is cur­rently im­pos­si­ble:

class Foo<T> : T
{
}

It’s sad, re­ally, how Mi­crosoft con­tin­ues to focus on other crap but leaves out basic things like con­straint-based over­load res­o­lu­tion or sim­ple in­her­i­tance. And still no Spec# fea­tures.

Visual Studio 2010 CTP now available

Co­in­cid­ing with the 2008 PDC, the first Vi­sual Stu­dio 2010 CTP is now avail­able for down­load. At first glance, it in­cludes a few in­ter­est­ing things for C++:

I’ll be post­ing more as I take a closer look at these and other fea­tures.

More on Japanese in Windows

If you just ripped your Japan­ese music col­lec­tion only to find out Win­dows Ex­plorer can’t dis­play any of the tags, you prob­a­bly used ID3 v2.4. Win­dows does not sup­port 2.4—if you down­grade the files to ID3 v2.3, every­thing will dis­play just fine. A good tool that can do this en masse is Mp3­tag. This doesn’t only af­fect the UTF-8 fields: Win­dows won’t be able to read album art or any­thing else if you use v2.4.

Visual Studio incompatibilities

Hope­fully some­one read­ing this won’t have to waste a few days try­ing to fig­ure this stuff out:

If you in­stall Vi­sual Stu­dio 2008 Team Ex­plorer, you’ll want to in­stall it be­fore VS2008 SP1 or stuff will break.

In­ter­net Ex­plorer 8 Beta 2 breaks the Win­dows Mo­bile 6 SDK Re­fresh—try to click on Plat­forms in the pro­ject cre­ation wiz­ard, and it’ll can­cel the di­a­log. Unin­stalling IE8 (Con­trol Panel->Pro­grams->View in­stalled up­dates->Win­dows In­ter­net Ex­plorer) fixes the issue.

Adding Japanese support to Windows Mobile 6.1

Windows Mobile with Japanese

The Win­dows Mo­bile 6.1 up­date just hit for my Black­jack II, and I’m lik­ing it quite a bit. One prob­lem, though, is that it seems to lack any Japan­ese fonts for the UI – every­thing shows up as those fa­mil­iar boxes. Here’s how to add the Meiryo font from Vista to your phone.

Caveat lec­tor: this in­volves mod­i­fy­ing the reg­istry on your phone. If you aren’t com­pletely con­fi­dent in your abil­i­ties with this, don’t do it. I’ve only done this on my AT&T Black­jack II run­ning the Win­dows Mo­bile 6.1 up­date. Phones are pretty damned ex­pen­sive, don’t blame me if you break yours!

You will need:

First you need to grab the font files from Vista, out of your Win­dows\Fonts dir. Ex­plorer doesn’t let you do this, so you can go via com­mand line or any other app with a Open File win­dow or some­thing sim­i­lar. You are look­ing for meiryo.​ttc and meiryob.​ttc. Once you’ve got these, copy them to your phone’s Win­dows\Fonts dir.

Next you’ll need to per­form some reg­istry edits on your phone. Open up Mo­bile Reg­istry Ed­i­tor and go to HKEY_LO­CAL_­MA­CHINE\Soft­ware\Mi­crosoft. Right click, and go to New->Reg­istry Key. Enter FontLink for the name. In­side of the FontLink key, add an­other key called Sys­tem­Link.

Windows Media Mobile with Japanese

In­side the Sys­tem­Link key, right click and go to New->Multi-String Value. For the Value Name, enter Segoe Con­densed. For the Value Data, enter win­dows\fonts\meiryo.​ttc,Meiryo. To my knowl­edge Segoe Con­densed is the only font used in the UI on Win­dows Mo­bile 6.1, but you can re­peat this step re­plac­ing Segoe Con­densed with any fonts you want to add Japan­ese sup­port to.

Now go back to HKEY_LO­CAL_­MA­CHINE\Soft­ware\Mi­crosoft. Add a new key called Font­Path. In­side this key, right click and go to New->String Value. For Value Name, enter Font­Path. For Value Data, enter win­dows\fonts.

Now just re­boot the phone and it should have Japan­ese sup­port through­out the UI!

RTM hits for SQL Server 2008, Visual Studio 2008 SP1, .NET 3.5 SP1

SQL Server 2008, Vi­sual Stu­dio 2008 SP1, and .NET 3.5 SP1 have all been RTMed!