Sunday, March 29, 2009

Debugging Memory Allocations

Debugging memory allocations etc is always a pain, especially if you have memory leaks which dont cause errors directly. There are a wide variety of tools which facilitate detection, the most useful being Valgrind. The tool is awesome, and it definitely helps in this task. Also GNU libc has a wide ranging support for just this task.

But unfortunately you are not always lucky, and might find yourself working on systems other than Linux. Like FreeBSD. (Well you might have guessed that I am no fan of FreeBSD). Although Valgrind has a port to FreeBSD, it coredumps on startup. So in such dire circumstances you are left with no choice but to start from scratch.

Well everyone knows the standard mymalloc method, which is just a wrapper for the standard malloc call. However here is a more efficient method of doing it. Use the linker option "--wrap,malloc" which will make any references to malloc point to __wrap_malloc() (which you will be writing) and to call the actual malloc from inside your wrapper just call __actual_malloc().

The other option is to use the LD_PRELOAD, which loads your custom library, so any malloc calls are resolved to your wrapper calls. To call the actual malloc(), use the statement - "dlsym(RTLD_NEXT, "malloc");", which will give a function pointer to the actual malloc().

I know the blog is not too comprehensive on how to use these 2 facilities, but now that you know what can be done, just google them :)

OPEC - Why it no longer calls the shots

"Are you nuts?" might be the first response of anyone who sees the title. Afterall everyone knows that the single factor which definitely affects the spot price of an Oil barrel on the floor of the NYMEX, the most, is an OPEC meeting. So let me elaborate. OPEC is powerful, but not as much as it once used to be. If anyone remembers the 1973 Oil shock - which brought America and most of the world to its knees in a decade long bout of stagflation, you will now know how much less power it wields today.

So what can be the reasons for such a powerful body's influence to wane? To understand the reasons, you need to understand the composition of OPEC. OPEC stands for Organization of Petroleum Exporting Countries. It is a cartel of 12 countries, the main member being Saudi Arabia accounting for over 25% of world reserves. Other countries like Venezuela have much smaller reserves. So now lets take a look at some of the reasons for the waning of its effects.

Production: Although OPEC used to control a lot of Petroleum production, New reserves discovered in the Gulf of Mexico, and Russia opening up its vast Oil reserves have reduced OPEC's global output to under 40%.

Price Issues: OPEC members have divided interests over Oil prices. While the naive reader might think that a higher price is in the best interests of all suppliers, things are more complicated. Saudi Arabia has much more Oil in the ground compared to other members of OPEC. So while a higher price helps in the short term, it will also accelerate development of alternative technologies, which will eventually reduce the world dependence on Oil. So OPEC members with less Oil in the ground, could not care less about the future, and want to make more money with less oil, and hence want higher prices. Saudi Arabia, on the other hand, wants the world to continue its addiction with oil, and hence would not want to invariably prop up alternative fuel technologies. This attitude is summed up by a quote by a Saudi Oil Minister as "The stone age didn't end because we ran out of stones". Even towards the end of 2008, Saudi broke ranks, and declared it will pump over its OPEC imposed production quota, to reduce the Oil prices which were in the stratosphere.

Infighting: Also after a great show of unity in the 1970s, the major OPEC nations turned against one another. Firstly the 8 year Iran-Iraq war distanced the Shia Iran from the rest. Later, Iraq which was trying to service its war debts more easily by rising Oil prices was rebuffed by Saudi and Kuwait which pumped out more Oil and depressed global Oil prices, finally provoking Iraq to invade Kuwait and eventually Saudi, if it were not for America's intervention.

The Iraq Factor: Although Iraq is a member of OPEC, it is not taking part in production quotas since 1998. And with the highly increased American influence, it is much more likely to do US's bidding at least in the near future, which most probably will not coincide with the official OPEC stand. Also after a long time, Iraq is comparatively stabler and will start pumping more and more Oil, increasing supply and indirectly reducing OPEC's influence.

America: And finally lets not forget US's Gunboat diplomacy by having a couple of warships and aircraft carriers patrolling the Gulf. Although Saudi is an ally, the war ships let it know its position, and the emergence of a Shia Iran, right next to its borders as well as a discontented population, rising Islamic terrorism against the ruling sheiks would make Saudi more reluctant to anger it ally America. And America is much more interested in the Gulf after the 1973 Oil Shocks.

These are some of the reasons why OPEC is less powerful, but lets not get carried away, It is still the single most powerful factor in determining global Oil prices. So let us hope that the dependence on Oil will decrease soon enough.

Tuesday, March 24, 2009

Oil prices scrapping the bottom - Lost Lessons


Now-a-days, Oil price is the last thing on people's mind, with an Election just over in the US, the economy in the dump, and theories about a very prolonged recession - which has even been compared to the Great Depression of the 1930s. Sure enough, it seems like almost no one remembers that oil prices were topping $147 just a few months ago... The commodity bubble has burst at last, and has dragged the price of Oil back into $50 range now.

So what now? Will the prices continue at $50? Obviously not. Once the downturn is over, the OPEC countries will actively seek their pound of flesh. So here is a golden chance for Obama to right many wrongs that were done earlier. Instead of seeking short term quick fixes for bumping up Oil production by offshore drilling or invading other countries, a comprehensive plan to rid America of its Oil addiction needs to be worked on.

Although the end of the cold war made the world a much safer place, it inevitably resulted in America losing its focus on Science & Technology. Ofcourse the top technology companies are still US-based ones, but they are private. And private companies tend to be short sighted for obvious reasons. And real technologies take time to mature and generate profits. It is the federal govt's responsibility to fund such long-term technologies, which will help tap into renewable resources.

The recession is showing at least some signs of waning, so once again a message from the Oil crisis is being lost. Renewable sources are the way to go, and still more research and funding into better batteries, solar power, etc is needed. Lets hope Obama does not lose sight of the future while trying to save the present.