The officers of the British and Canadian militaries used to come from aristocratic and upper class backgrounds. Today, serving in the military has become less prestigious and very few from the upper class actually serve. When and why did this change occur?
I can only provide some info from a British perspective:
The Cardwell reforms in 1870 marked a departure from purchased commissions in Britain, and is often described as a reaction to the franco-prussian war, which showed the superiority of professional soldiers over "gentleman soldiers".
These reforms had roots in the crimean war, where severe mismanagement of human resources and misadmimistration had been highlighted in the ranks of the officers and war office, and had been brewing for some time, and was later reinforced by wwi, where officer deaths were far too common to be filled entirely by the upper class.
How do you know whether the number serving is up or down?
This question needs to be taken apart a bit to be answered properly.
First, you should question your assumption that being a serving officer has lost any of its prestige. Ex-first-term and second-term officers abound in fast track positions throughout business and government in Canada and the UK; there is no better resume if one is seeking selection to stand for parliament, for example. Career officers invariably go on from retirement to highly responsible positions or well-paid sinecures, as they like.
Second, the aristocracy constituting a lower percentage of the officer corps reflects a dramatic change in the nature and prestige of the aristocracy. The aristocracy used to possess immense political and economic power and military service operated to preserve that position, reward that position, and, constituting a dangerous public service, at least somewhat mollify a working and middle class that might otherwise see the aristocracy as predatory / parasitic / oppressive (what have you). The diminution of the economic and political status of the traditional aristocracy has naturally operated to reduce that complex mix of incentives to serve.
Third, the aristocratic military tradition took quite a hit in the World Wars, when the deadliness of combat really ramped up. The Western Front of the First World War was a killing field that British and Canadians had no great experience of, and it especially took its toll on young officers. If you read through the Hansard for 1914-1919 the references to the battlefield deaths of young peers and commoner members of aristocratic families become numbing (and you are also reminded of how aristocratic the Commons was, to say the least of the Lords). By 1945, aristocratic families had gone through two generations of deaths and dismemberments that doubtless deeply impacted their enthusiasm, and then the new Labour government proceeded to enact death taxes which they knew very well would break the fortunes of all but the wealthiest and tax-savviest of aristocratic families. The basis of noblesse oblige was greatly eroded on all sides.
Fourth, the role of a military officer has become an ever-more sophisticated one. It make a lot more sense for the military to focus its recruiting on the patriotic, high IQ sons of middle class strivers; that they are by the way (perhaps at a generation's remove) to share the accents and the family background and experience of the rankers they will command is not a bad thing either.
I could chime in about the professionalisation of the General Staff (although unfortunately not from the British, or Canadian perspectives), but I'll give it a gander.
Essentially the root of the question seems to be asking about the professionalisation of the Officer Corp (and its move away from the aristocracy).
The very macro explanation of it deals with the expanding complexities of managing the state. The Industrial Revolution was occurring around the same time as European states experienced massive population growth (outpacing most of the world). The functions of what an Officer was soon became distinct from other roles it previously shared.
Specifically speaking, the militaries of Europe were undergoing a small organizational revolution of their own. Beginning with the creation of the Prussian General Staff (and its predecessors that emerged from the Prussian's 1806 Military Reorganization Commission), the military began to see the benefits of unified military planning. Officers were expected to have a certain proficiency in military planning (and unofficially existed the expectation to have graduated from the Prussian Military Academy). Such reforms had created an efficient way to manage the planning of the logistical, operational, and strategic needs of the military. With the Prussian victories against the Austrians in the 1866 Austro-Prussian War, and the French in the 1871 Franco-Prussian War many European militaries began to seriously reconsider their military organization and adopt the Prussian model (some countries, such as Denmark, would emulate the Prussian model earlier).
Another factor would be the pervasiveness of democratic and liberal ideals in the aftermath of the Atlantic Revolutions. The aristocratic-commoner divide played a significant role in the French and Prussian militaries, which required reformers, and aristocratic conservatives to reach the compromisable position of the mutual recognition of an independent Officer Corp, free of class, or political allegiance. This was opposed to certain other states where the problem is neither as stratified or pervasive (such as the United States and the United Kingdom). In Britain, the push for professionalisation didn't begin until the 1870 Caldwell Reforms (and they didn't formulate a General Staff until 1904). In the US, there was actually a push against professionalisation because of their own perceptions of it being aristocratic.
Simply put, the idea of the Officer was evolving. They were no longer expected to be only hobbies of the aristocrats, but a profession. Of course in saying all this, I'm not suggesting that the changes occurred overnight. I mean in the 1866 Austro-Prussian War, the aristocracy still made up 2/3rds of the Prussian General Staff (although many were also graduates of the Prussian Military Academy). Also the aristocracy in the military wasn't something that was gone by the 20th century. A study on the Dutch Army's composition from the 1890s to the 1950s found that members of the nobility were still active in the military, with a significant concentration in Calvary as late as 1951 (although those numbers showed a steep decline).
Regardless of this though, it would be because of these factors that the other classes would be able to assert themselves in the military, and the Officer Corp.
I think we need to define "upper class" here: In 2013, just under half of the British Army's entire officer corps had been through private education. Now, I would daresay that's the lowest it's been for many, many years, but that does mean that nearly half of all our officers are drawn from less than 10% of the entire population, private schooling in the UK still largely being the preserve of the wealthy.