Global Variables are Bad (Usually)

August 14th, 2007

I’m working on an embedded software product that decodes network video and renders it to a monitor.  The application is designed to run 24×7, with up to four streams running through it at a time.  I didn’t write all the code from scratch, I took it over from someone else and am now responsible for bug fixes as well as advancing the functionality. When I inherited the project, I also inherited a bug that was caused by a global variable.  I had made almost a dozen releases with this bug, but it never manifested itself.

The global variable was a wrapper written around the sem_t that handled all of the initialization, destruction, etc.  Someone went into an old C style source file and threw a global instance of that class to provide multi-threaded support.  That might seem fine and dandy, but when unloading the library from memory, a race condition existed that wasn’t very obvious.  Some functions needed to be called in the library during cleanup that relied on the semaphore.  As long as that code was able to execute before the global variable was destroyed, everything worked fine.

The problem is, you have no guarantee of the destruction order of objects.  So, after a bunch of problem free releases, some subtle change had caused the semaphore to be destroyed before the library finished cleaning up.  The application would crash then once the library started to clean up.

I enabled core files and it became pretty obvious that the problem had to do with the semaphore.  It wasn’t the most disgusting global variable problem one might encounter.  I’m just so surprised that people continue to use globals so freely.  I’m positive whoever put that in there did it because it was quick and easy.

There’s no reason that this C file couldn’t have been turned into a class.  It would have only taken a few minutes.  If this person felt that the sem_t was really needed globally to these functions, they could have used some quick regular expressions to make the proper conversions.

So, when is it okay to use a global variable? Almost never.  I can’t give you an example off the top of my head because it’s almost never appropriate.  If you can think of a good example, post it as a comment.

RIAA Out Of Control (Again)

August 6th, 2007

So the RIAA has gone after Yuri Shutovsky for copyright infringement.  Our good friend Yuri (no, I don’t really know him) has an alibi during the time that this alledged copyright violation occured.  It seems that he was on an extended trip to Russia.   He even offered up his passport showing that he has entry and exit stamps proving that he was in Russia during this time.

Not to be deterred by such trivial details, the RIAA has asked a judge that Yuri turn over all hard drives in his possession as well as the names and addresses of everyone who has ever used his computer.  Unsuprisingly, the judge has sided with the RIAA and has required that he turn over all of this information.  They even asked (very politely, I’m sure) that his work place be required to turn over all information about any computer that he has ever used while in their employ.

Even though Mr. Shutovsky was in Russia during the time of the alledged violation the RIAA believes that  “regardless of who was using the PC at the time, Yuri Shutovsky is responsible for whatever copyright infringement may have occurred by virtue of his paying for broadband from Verizon” (Arstechnica).  By that very argument, Verizon is also responsible for whatever copyright infringement occured because they allowed someone to commit copyright infringement through their network.

But, we all know that RIAA would never go after Verizon.  Why? Because Verizon has enough money that they can afford to hire attorneys competent enough to defend themselves against such a baseless lawsuit.

Here’s a tip to all you record labels out there.  Quit trying to scare your customers into you buying your product.  I know that downloading pirated music is illegal and I honestly don’t understand why people choose to do so.  I can’t understand it because I can’t understand why most people would want to listen to the crap that you sell.  Instead, focus your time, energy, and money on finding and promoting music that is worth buying.  Once you do that, learn to charge a reasonable fee and guess what? People WILL pay for your music.

Scaring your “customers” into paying for your service reminds me an awful lot of the racketeering performed by the Mafia.  In fact, maybe Mr. Shutovsky should sue the RIAA for racketeering?  I’d love to see that civil suit.

Stack Trace in Linux

June 22nd, 2007

This is incase I forget how to print out my current stack in linux:

#include <execinfo.h>

size_t sz;
void *bt[20];
char **strings;

sz = backtrace(bt, 20);
strings = backtrace_symbols(bt, sz);

for(int i = 0; i < sz; ++i)
fprintf(stderr, “%s\n”, strings[i]);

TorrentSpy Ordered to Log Ram??

June 21st, 2007

So if you check out this article on ZDNet you’ll see that a Los Angeles judge has ordered TorrentSpy to start logging all site traffic and to turn over all data stored in RAM on their servers. This is the most absurd thing I have ever heard of. The article on ZDNet likens this to having to save post-it notes but it goes far beyond that. I would liken it to a person having to store every single thought that they had. The brain, like the RAM on a computer, works far faster than a pencil, keyboard, or a hard drive. That means that the brain, like the computer, would be forced to work at the speed of your fingers. Can you imagine having to think at a rate of 50 or even 100 word per minute? Absolutely absurd.

Anyway, if you folks at TorrentSpy are reading this, here are my suggestions for you. First of all, absolutely insist that the MPAA have to provide you with all the hardware and any software needed to log this. Secondly, insist that any software they provide you be open source and that it be peer reviewable on the internet. This is to protect you from having the MPAA do anything malicious or illegal. I’d then also get the judge to allow you to only put this logging on half of your servers. You, in good faith, would not modify your network structure to keep those servers from receiving less connections than they can handle. We all know they won’t get that much traffic anyway, they will be completely bogged down. Finally, to show just how absurd logging RAM is, I’d suggest you turn on encryption for all server traffic. Might as well make the MPAA sort through thousands and thousands of pages of factoring data. This will cause server performance to be so slow that these servers will only get a few hits per hour. It would affect very little of your clientel.

Good luck to you TorrentSpy! While I never condone piracy (I do work in the software industry, after all), I believe that the RIAA and MPAA have gotten themselves into their own mess by churning out complete CRAP for the last 10+ years. If they actually put out a decent work, maybe people would pay to see it in the theater and even pay to have a copy at home? A good movie is worth $10-15. But, ninty-nine percent of the movies produced these days aren’t even worth the cost of the film they are stored on.

P0171 Check Engine Code on 2000 Toyota Corolla Part 3

June 20th, 2007

Ok, after reviewing the previous charts a few times, I decided that the Mass Airflow Sensor and the second Oxygen sensor were not behaving the same when my check engine light would turn on. That is to say that I could follow the two graphs and they tended to behave the exact same way when everything was normal. When the problem would occur, my MAF sensor was indicating that the air intake was decreasing while the second oxygen sensor was indicating that oxygen was increasing. This suggested to me that the MAF was not reading the amount of air properly and so we were running lean, which caused an upward spike of available oxygen in the air.

I bought a reconditioned MAF sensor for about $80 + $50 core and replaced it on my car. It only takes about 5 minutes to do. I reset the computer and took it for a test drive. The performance seemed better at the time but it was hard to say because the computer had to readjust itself.

It has been about 220 miles and two week now since I replaced the sensor. The cars performance is good, it accelerates more smoothly and has more power. I haven’t put in a new tank of gas yet, so I have no idea if the gas mileage has improved.

So, if you have this problem and cleaning the MAF sensor doesn’t seem to work, that does not mean that the MAF isn’t the problem. Mine seemed to completely malfunction randomly and cause horrible performance. The gas mileage had dropped significantly even though the check engine light didn’t always turn on.

Work Email Address Hijacked

June 11th, 2007

That’s right, someone stole my work email address. The culprit was no hacker though, it was the CEO of my company who took it from me. Why would he want my email address? Well his administrative assistant has a name that is almost identical to my own. In fact, the only difference between our first and last names is one letter.

His assistant and I have peacefully coexisted ever since she started working at our company about 6 months ago. Occassionally, someone inside the company would incorrectly send emails to her instead of me. The reason they do this is because they use auto complete to type in the address for them automagically and my name comes before hers alphabetically.

For reasons unbeknownst to me, he decided this morning that I needed a new email address. His office didn’t contact me, ask me how I felt, anything like that. I got an email from our IT department asking me what my middle initial was so that they could adjust my address. Of course, several months ago his assistant asked me if it was okay for us to switch addresses. I objected then since I had been using that email address for about 3 years and felt that it was too inconvenient since the problem only seems to be an internal one. Changing email addresses wouldn’t fix an internal problem because they are using their address book auto complete, and not just typing in the wrong address. So, his office already knew that I was oppossed to the idea.

Anyway, so I responded to our IT departments email and made sure that the President’s assistant got my response. I told them that I believed strongly that the problem was caused by people being careless, and not because of my address. I also told them that the change would be rather inconvenient because I couldn’t remember all of the mailing lists I was subscribed to, that it might inconvenience customers, vendors, and other people, but the IT department said their hands were tied.

That wasn’t at all surprising. I just think that it is absurd that my email address of many years hsould have to change because of a perceived problem by the CEO. I have several friends who have had to deal with this problem for over 4 years. I’m willing to bet $1000 that it doesn’t even fix the problem. I’m going to laugh if I get some super confidential email after all this fuss was made over my email address.

The best part of all was that the IT department didn’t even tell me before they made the change. They just did it and it caused my email to go down. I was without email for hours because of this.

P0171 Check Engine Code on 2000 Toyota Corolla Part 2

May 31st, 2007

Alright, I got a more sophisticated ODB-II reader. I borrowed it from my brother. I was able to get more advanced diagnostic information as I was driving the car. I took a reading of “normal” sensor data. It shows the engine RPM, the mass air flow, and the output from the 1st and 2nd oxygen sensors. At the time this capture was made, the car was going from about 65mph to a dead stop.Normal sensor data

The second capture was taking within a minute or so of the check engine light coming on. I’m going approximately 65mph thru the duration of the entire graph.Sensor data within 1 min of check engine light turning on

Whisper Aire 2000 whisper quiet

April 29th, 2007

Some of you may remember how at the same time I started this site, I also installed the Whisper Aire 2000 whole house fan. In that post I complained about how it was a bit nosier than I expected it to be. Well I’m able to tell you now that the fan is so quiet you wouldn’t even know that it was on.

Well, that could be that it’s just not working. I used it about a month ago without any problem and now it doesn’t seem to do anything at all. I’ve contacted the company but their website doesn’t even list the product anymore. That’s not a good sign. It makes it seem like they’ve had so many problems with this product that they just want to pretend that it never even existed. Or, at the very least, that they had nothing to do with it.

In any event, I am currently cranky about the whole thing. I went up into the attic and made sure there were no visible problems with the wiring and what not. It’s on the same circuit as my internet so it must have power. I even flipped the thing off, waited and flipped it back on to see if maybe some breaker needed to be reset. The thing is still dead.

The joys of interlaced video

April 25th, 2007

So the world is moving from SD television to HD television.  There are now several different video formats available to broadcasters incuding 480p 720p and 1080i.  Many people wanted to move away from interlaced video completely and only support progressive scan video. But, our government, in its infinite wisdom, standardized everything at 1080i.  I don’t understand that at all.  Interlaced video creates all sorts of interesting challenges, espcially in the era of digital content.

I have a problem at work right now that is specific to interlaced video that I cannot figure out how to overcome.  My problem is that I need to draw basic primitives to the overlay channel.  These primitives aren’t supposed to obstruct or detract from the video but are generated based on user defined areas and the video’s content.   These overlays have to be drawn on coax (via NTSC/PAL), or a S/VGA display.

The VGA output is non-interlaced while the coax is interlaced.  What this means is that my primitives look absolutely beautiful on VGA but have a very annoying flicker over coax.  For those of you unfamiliar with interlaced versus non-interlaced video it is because a VGA monitor draws the entire screen every time it paints.  A video monitor (TV, etc) is interlaced and draws every other line and then goes back and draws the lines that it skipped the first time.  These two different draw periods are referred to as fields.

This means that if you had a horizontal line that was one pixel wide, it would be drawn in  one field and not drawn at all in the next field.  This causes the line to disappear, only to be drawn again.  That is where the flicker comes in.  Just doubling the thickness of the line would likely result in having two lines flickerin on and off right next to each other.

Well, I don’t have a problem with my horizontal lines, but I do with diagonal lines.   What I am doing is this.  If I am going to turn on pixel (x,y) then I would also turn on (x,y+1).  I’d then paint pixels (x,y+2), (x,y+3), (x,y-1) and (x,y-2) with the gradient color.  That way every field will have one pixel for the line and one pixel on each side of the line for the gradient color.  That combined with the persistence of the phosphors in the previous field should blur everything to the eye and make it look more consistent.  At least, that is what I would think.

I’m still getting flicker, however.  Not only am I getting flicker, but it also seems to be worse on lines where the slope is greater than 1.

I’m absolutely perplexed by this and I can’t figure out how to resolve my flickering issue.  I’ve tried to decrease the intensity of the colors that I am using by half and that makes the flicker less noticable but does not remove it completely.

I’m completely stumped.  I’m going to go talk to one of our senior hardware engineers to see if he has any idea.  He knows analog video very well.  If he has any thoughts, I’ll be sure to share them.  Otherwise, please give me any thoughts you have.

xargs + sed

April 17th, 2007

I always forget how to chain xargs w/ sed. I use it so rarely that I always have to look it up again. Here its an example for future reference. Note that the command is `sed -i single quote single quote` not a double quote.

find . -name Makefile | xargs sed -i ” -e ‘s/–parents/-p/’