CLR Profilers and Windows Store apps

Last month Microsoft published a white paper about profiling Windows Store apps. The paper is very detailed and provides rich information how to build CLR profiler for Windows Store apps. I was very curious to read it because at the time when we released JustTrace Q3 2012 there was no documentation. After all, I was curious to know whether JustTrace is compliant with the guidelines Microsoft provided. It turns out it is. Almost.

At time of writing JustTrace profiler uses a few Win32 functions that are not officially supported for Windows Store apps. The only reason for this is the support for Windows XP. Typical example is CreateEvent which is not supported for Windows Store apps but is supported since Windows XP. Rather one should use CreateEventEx which is supported since Windows Vista.

One option is to drop the support for Windows XP. I am a bit reluctant though. At least such decision should be carefully thought and must be supported by actual data for our customers using Window XP. Another option is to accept the burden to develop and maintain two source code bases – one for Windows XP and another for Windows Vista and higher. Whatever decision we are going to make, it will be thoroughly thought out.

Let’s have a look at the paper. There is one very interesting detail about memory profiling.

The garbage collector and managed heap are not fundamentally different in a Windows Store app as compared to a desktop app.  However, there are some subtle differences that profiler authors need to be aware of.

It continues even more interesting.

When doing memory profiling, your Profiler DLL typically creates a separate thread from which to call ForceGC. This is nothing new.  But what might be surprising is that the act of doing a garbage collection inside a Windows Store app may transform your thread into a managed thread (for example, a Profiling API ThreadID will be created for that thread)

Very subtle indeed. For a detailed explanation, you can read the paper. Fortunately JustTrace is not affected by this change.

In conclusion, I think the paper is very good. It is a mandatory reading for anyone interested in building CLR profiler for Windows Store apps. I would encourage you to see CLR profiler implementation as well.

Profiling Tools and Standardization

Imagine you have the following job. You have to deal with different performance and memory issues in .NET applications. You often get questions from your clients “Why my application is slow and/or consumes so much memory?” along with trace/dump files produced by profiling tools from different software vendors. Yeah, you guess it right – your job is a tough one. In order to open the trace/dump files you must have installed all the variety of profiling tools that your clients use. Sometimes you must have different versions of a particular profiling tool installed, a scenario that is rarely supported by the software vendors. Add on top of this the price and the different license conditions for each profiling tool and you will get an idea why your job is so hard.

I wish I can sing “Those were the days, my friend” but I don’t think we have improved our profiling tools much today. The variety of trace/dump file formats is not justified. We need a standardization.

Though I am a C++/C# developer, I have a good idea what is going on in the Java world. There is no such variety of trace/dump file formats. In case you are investigating memory issues you will probably have to deal with IBM’s portable heap dump (PHD) file format or Sun’s HPROF. There is a good reason for this though. The file format is provided by the JVM. The same approach is used in Mono. While this approach is far from perfect it has a very important impact on the software vendors. It forces them to build their tools with a standardization in mind.

Let me give you a concrete example. I converted the memory dump file format of the .NET profiler I work on to be compatible with HPROF file format and then I used a popular Java profiler to open it. As you may easily guess, the profiler successfully analyzed the converted data. There were some caveats during the converting process, but it is a nice demonstration that with the proper level of abstraction we can build profiling tools for .NET and Java at the same time. If we can do this then why don’t we have a standardization for trace/dump files for .NET?

In closing, I think all software vendors of .NET profiling tools will benefit from such standardization. The competition will be stronger which will lead to better products on the market. The end-users will benefit as well.

Rants on Education

In this post I am going to share my thoughts about the education and some of the current educational approaches. I was provoked by an article by I. V. Arnold published in the Russian magazine Математическое просвещение year 1936, issue #8.

The article was about choosing a winning strategy for the following game (I don’t know the name of the game so if you know it please drop a comment; I think the game origin is from Japan or China). Here is the game: there are two players and two heaps of objects. Let’s call them player A, player B, heap A (contains a>0 objects) and heap B (contains b>0 objects) respectively. Each player takes a turn and removes objects from the heaps according the following rules:

  • at least one object should be removed
  • a player may remove any number of objects from either heap A or heap B
  • a player may remove equal number of objects from both heap A and heap B

Player A makes the first turn and the player who takes the last object(s) wins the game. Knowing the numbers a and b you should decide whether to start the game as a player A or to offer the first turn to your opponent.

Sometimes this game is given as a problem in mathematical competitions (or less rare in informatics ones). When I was at school we studied this game in math class. Back then I was taught a geometric based approach that chooses a winning strategy. I will skim the solution here without going in much details because this is not the purpose of the post.

As the game state is defined by the numbers a and b we can naturally denote it with the ordered pair (a, b). Then we can naturally present the game state on a coordinate system. Let’s say a=2, b=1 so we have (2, 1) point on the coordinate system and let’s put a pawn on this point.

According the rules we may move the pawn left, down and diagonally towards (0, 0) point. It’s easy to see that it is not possible to win the game if we start from position (2, 1) so let’s define position (2, 1) as a loosing position (LP). I am going to provide the list of all possible moves:

  • (2, 1) → (2, 0). The next move (2, 0) → (0, 0) wins the game
  • (2, 1) → (1, 1). The next move (1, 1) → (0, 0) wins the game
  • (2, 1) → (0, 1). The next move (0, 1) → (0, 0) wins the game
  • (2, 1) → (1, 0). The next move (1, 0) → (0, 0) wins the game

Point (0, 0) should be also considered as a loosing position. We notice that if (a, b) is a loosing position then (b, a) is also a loosing position. To find the next loosing position we have to draw all horizontal, vertical and diagonal lines from all existing loosing points and find the first point not laying on those lines.

Using this approach we see (5, 3) is a loosing position while (5, 1) is a winning position (WP). The strategy is already obvious – if the starting position is LP we should offer the first turn to our opponent, otherwise we should start the game and move the pawn to a loosing position.

So I was given an algorithm/recipe how to play and win the game. Back then my teacher said that the game is related to the Fibonacci numbers but she did not provide additional information. It wasn’t until I read the article mentioned above and I’ve finally understood the relation between the game and the Fibonacci number (and the golden ratio in particular).

Back to the main topic of this post. 20 years ago my teacher taught me how to solve particular math problem. I was given an algorithm/recipe that explains the winning game strategy. Indeed it is true that I didn’t know why exactly the algorithm works but still it was presented in a very simple and efficient way. In fact I knew it for the last 20 years. This triggered some thoughts about here-is-the-recipe kind of eduction.

It seems that here-is-the-recipe kind of education works pretty well. My experience tells me so. Most of the time I’ve worked in a team and I had plenty of chances to ask my colleagues about things in their expertise. Sometimes I was given an explanatory answer but most of the time I was given a sort of this-is-the-way-things-are-done answer. And it works. So I believe that here-is-the-recipe kind of education is the proper one for many domains. Not surprisingly these domains are well established, people have already got the know-how and structure it. Let me provide some examples.

One example are design patterns. Once a problem is recognized as a pattern a solution is applied. Usually applying the solution does not require much understanding and can be done by almost everyone. Sometimes applying the solution requires further analysis and the if not done properly the result could be messy. This is very well realized by the authors of AntiPatterns: Refactoring Software, Architectures, and Projects in Crisis. They talk about 1980’s when there were a lack of talented architects and a disability of the academic community to provide detailed knowledge in problem solving. I think that nowadays the situation is not much different. One thing for sure – today the IT is more dynamic than ever. Big companies rule the market. They usually release new technologies every 18-24 months. Many small companies play on the scene as well. They (re)act much faster. Some technologies fail, some succeed. It is much like Darwin’s natural selection. The academic education can not respond properly. Fortunately today’s question-and-answer community web sites fill that gap especially for the most common problems.

Another example are the modern frameworks and components. I’ve seen many people using them successfully without much understanding the foundations. Actually this is arguable because one of the features of a good framework/component is that one can use it without knowing how it works. Of course sometimes it is not that easy. Sometimes using a framework requires a change in the mind set of the developer and it takes some time. In such cases the question-and-answer community web sites often recommend not the best (sometimes even bad) practices. I guess it all depends on the speed of adopting the framework. If many people embrace the new framework/technology the best practices are established much faster.

On the other side sometimes here-is-the-recipe kind of education does not work. As far as can I tell this happens for two major reasons.

The first one is when a technology/framework is replaced by a newer one. It could be because it is too complex and/or it contains design flaws or whatever. One such example is COM. I remember two books by Don Box: Essential COM (1998) and Essential .NET (2002). The first one opens with “COM as a Better C++” while the second one opens with “The CLR as a Better COM”. Back then the times were different and four years were not a long period. Although COM was introduced in 1993 the know-how about it was not well structured.

The second reason is when the domain is too specific and/or too rapidly changing. In case of too specific domain there is not enough public know-how. The best practices usually are not established at all. In case of rapidly changing domain even if there is a know-how there is not enough time to structured it.

Of course no teaching/education methodology is perfect. So far I think the existing ones do their job as expected. Though there is a lot of space for improvement. Recently I met a schoolmate of mine who is a high school Informatics teacher. We had an interesting discussion on this topic and I will share it in another post. Stay tuned.

Invention and Innovation

This is an old topic but I would like to write a post on it. One of the common arguments is that an idea that doesn’t change the behavior of the people is only an invention, not an innovation. Here is a quote from Wikipedia:

Innovation differs from invention in that innovation refers to the use of a better and, as a result, novel idea or method, whereas invention refers more directly to the creation of the idea or method itself.

In our history you can find many examples for inventions that become innovations after years or centuries. Sometimes the inventions are made too early. Typical examples are the radio and the TV. For other ideas the transformation from invention to innovation takes only a few days. Last but not least, there are innovative ideas as well.

Often the inventor and the innovator are different persons. In this post I am going to focus on the information and knowledge spreading between the people that is needed to transform an invention to an innovation.

Today there is a clear connection between the inventors and the innovators. Usually they are both professionals in the same or closely related fields. For example I am not aware of an innovation made by a journalist that is based on an invention made by a medical doctor. I guess there are examples for such cross-field innovations but they are not the majority.

Because these information and knowledge flows are quite narrowed I start wondering how much innovation opportunities are missed. The naive approach is to use internet for sharing. There are many web sites that serve as idea incubators/hubs but I think these sites alone are not enough. The main problem of these sites is that currently the ideas are not well ordered and classified. One possible solution is to use wikipedia.org site. The main advantages are:

  • a single storage point
  • a lot of people who are already contributing

Volunteer computing projects, such as SETI@home, might be of a use as well. Automatic text classification is a well developed area though it cannot replace human experts yet.

Online games such as Foldit should be considered as well. If there are games that offer both fun and sense of achievement then they might attract a lot of people to classify ideas and actually generate innovations and other ideas.

As a conclusion I think that with the technologies and the communications available today and a little effort it is easy to build a healthy environment for generation of innovations.