Eclipse, SDL and linking HELL

I am using Linux Mint 64 bit system.
I started writing some base code using SDL2 and I wanted to try out Eclipse; you know IDE and stuff instead of good old VI. The default way I use to include and link SDL2 to my application is to pass `sdl2-config –cflags` to the CXXFLAGS and `sdl2-config –libs` to the LDFLAGS.
Eclipse, in all its glory gave me a way to pass the include flags.
Project->Properties->C/C++Build ->Settings->GCC C++ Compiler->Miscellaneous-> “Other Flags” and just append `sdl2-config –cflags`
This works.
Then comes the linking part because I kept getting SDL_Init “unresolved symbol” and no matter what I tried it did not work. After a lot of fighting I realised that
Project->Properties->C/C++Build ->Settings->GCC C++ Linker->Miscellaneous->Linker Flags -Xlinker provide the linking files *BEFORE* the .o files are linked. What I needed was a way to provide the `sdl2-config –libs` *AFTER* all the .o files are listed. So for anyone out there the solution I have for now is to append `sdl2-config –libs` to the value in
Project->Properties->C/C++Build ->Settings->GCC C++ Linker-> “Expert settings: Command line pattern”

Very inconsistent. I hope this gets fixed soon.
Ofcourse if anyone thinks there is a better way to do this, I’d welcome comments

The case of mysterious frame drops

I play Team Fortress 2 on a regular basis and I own a monster of a system for what TF2 requires (read: Alienware M14X). I never really had any problem with running the game at a minimum of 60fps on the highest settings. And this has been the case for the past 3 years or so.

So about a month ago when I started seeing weird behaviour and specifically massive framedrops I wondered what was going on. It so happened that at the same time there was a patch from Valve that messed up the game. It caused crashes for a few, dropped frame rate for a others and straight up rendering artifacts for many others. I had the latter two checked on my system.

This coincidence was the first thing that threw me off. I was genuinely bothered by the bugs on TF2 but I decided to leave the game alone for a week or so. So when I logged back and I saw a patch I was happy because it was my saving grace, right? Wrong. While it definitely fixed the bugs and artifacts I still had terrible frame rates. I am talking about 6fps to a best of 30 fps when other players were visible. The weird part was that I was hitting 120fps when I faced away from everyone in the spawn room. I could not attribute this behaviour to anything I could understand.

Getting all technical:

A long time ago I worked on Xbox 360 and optimisations for the game we were working on. So I thought, well TF2 is no different. Off I went and downloaded the Intel GPA. It was rather simple the last I used about 4 years ago but now it was very different and a lot more complicated. So I started with the tutorials. Quite frankly there are some very nice tutorials that Intel provides. It took me about 1 day to pick all of it up and then I started working on TF2.

The first thing I did was setup a trigger for when the fps would drop below 25fps. That did not take too long. The question I wanted to answer was is it a CPU or a GPU bottleneck that I am observing. The GPU was taking close to 110ms to render a frame and that made no sense. The GPU can certainly take the flimsy shaders. So then I went into the game, turned all the features down, went to DX8 and started the game again. Certainly it wasn’t the same scene that I was profiling against but I did notice that the GPU was still at around 100 ms mark. This made no sense so I started looking at the stall times. Bing there were a crazy number of stall times and idle calls.

So then I looked at the CPU. I could not really get any good counters going here but the in game HUD from Valve did the trick. There were a significant number of draw calls but still nothing that really mattered. It appeared that the CPU was not pumping enough stuff out the pipeline. This was a mystery because I knew the CPU was quite beefy for the game.

Could this be another case of buggy Valve code?

It was a compiler error!

No it is not a compiler error but I’ve come across too many people attribute buggy code to compiler errors. I did encounter a compiler bug but that was when we were still working on early compiler releases for Xbox 360. No I never encountered compiler errors after that.

However blaming Valve for buggy code is similar to blaming compiler errors. When Valve pumps out buggy code or slow code enough number of people complain it that the forums will light up like an Alien on the Marine’s motion tracker. None of that happened. At this stage I was quite sure that there was something amiss on my system.

To confirm this I installed TF2 on my Desktop (Pentium D + Geforce 9800GT). That little bugger ran the game at the expected 120fps on lowest settings. This was when I decided to check what else changed on my laptop.

I suspected the CPU at this stage to be the primary bottleneck so I wanted to first benchmark it. I downloaded SiSoft’s benchmarking tool and ran it. Then I compared it to the numbers posted for my CPU Intel i5 2450 and bingo there was something really wrong with the CPU. It just wasn’t performing well.

Was it a Flu?

With the Flu season going around I suspect a Virus infection (wink wink @ Independence Day). There are only so many things that could cause it. I could safely rule out the Antivirus (Avast) because I had actually placed exceptions for TF2 and the child directories. I did a system wide scan and found no Viruses either. I could safely rule out a Flu for my system.

At this stage I installed Linux Mint anyway because I was working on Firefox OS development and working in the VM was getting rather painful. So I installed TF2 on Linux and I noticed very similar behaviour. I was not sure if it was the nVidia Optimus drivers messing around but I did knock this off by checking the GL strings. It was the nVidia GPU that was active.

Eureka!!

Now I must attribute this find to something completely unrelated to any of the profilers. While I was working in Linux I started a video (GTC videos) that ran at 1080p. After about 10 mins of running the system just shut off. This was weird and I wasn’t sure if it was my Son who knocked a cable out or something (I don’t run on Battery). I immediately checked the power settings to make sure that the system was not holding any CPU cycles back. No luck there either.

At this stage I was out of ideas but the profiler and benchmark numbers kept bothering me. Why was the CPU a bottleneck? So today when I started the GTC video again and the system shutdown I wondered if this was due to the CPU running too hot.

I installed gkrellm and noticed that the bare temperature was ~75C. Now this is ridiculous considering it is Winter and we live in the Midwest. My office room is 20C. While CPUs do run at a higher temperature this much of a variance on a cold start system made no sense to me. But, if only, the CPU was not being cooled enough.

Happy ending:

So opened up the laptop and sure enough there was enough dust there to cause a mutation and prove to everyone, once and for all, that Evolution is real. So I cleaned up the fan with a vacuum. Restarted the system and checked the temperature. Right now as I am typing this up I see 38C. When I started it, the temp read 35C.

My next test was to start the GTC video again. Well the temperature bumped to 45C and the GPU stood at 38C. Those are more like the numbers I am used to. Also I noticed that system was rather quiet.

For the ultimate test, I started TF2 (in Linux) and well guess what I can get back to being a Sniper. A steady 60fps. So children everywhere learn from my mistakes. Do give your computer a good clean at least once an Year. That and stay in School, till someone has a better advice.

Cars

I just realised that I’ve been posting more on Twitter than on my blog. Must fix this.

Anyway lately I’ve change the things that pull me. Considering the crazy cold weather here I had decided to make the most of the warm weather and perform some physically taxing activities. Included are taking the kids for a walk and having to chase them.

More than that I started and for the first time in my life actually worked on cars. Yes. Cars. Something that very few folks from Software world would imagine although the first things in OOPS is the car analogy. I can say with reasonable amount of confidence, that the analogy is based on simplifying the problem domain and I do not think the analogy answers questions appropriately anymore.

I purchased a 2008 Maxima and it had significant hail damage. I am not fixing any of the damage. It is a high value car for the simple reason that its powertrain is in a good shape and it is the last of the 6th generation Maximas. The engine VQ35E is pretty much bullet proof and best of all has a timing chain.

Lam, a colleague at work, knows his way around a car. I am quite lucky to have spotted him and he’s mentored me in quite a few aspects around the car. With Youtube for theory and Lam for reviewing my work I managed to work on quite a few pieces including wheel bearing hub, rotors, brake pads, electrical work on the window, inner fender liner and a bunch of other pieces. More importantly I mustered up enough courage to help Rakesh, another colleague, replace his serpentine belt. This means a lot to me and sometime after this Winter I’d like to get going with a proper engine swap or even better an engine rebuild. Hopefully on a Subaru as I love those cars.

Batik, SVG and weird javascript errors

I am using Clojure to generate SVG documents. To do this I use a library called lacij which in turn uses tikkba; a wrapper around the Apache Batik SVG generation library.

I added support for embedding javascript inside the SVG document (not the html) by manipulating the DOM as described here http://stackoverflow.com/questions/17906992/adding-script-to-lacij-graph

The problem was that I was getting weird svg error “unknown language text/ecmascript”. As it turns out I was using the wrong xlink namespace and posted the answer on the above mentioned thread.

The code I wrote now is like this:

(defn build-svg-graph [graph]
(def new-graph(-> graph
(LALayout/layout :hierarchical)
(LAGraph/build)
)
)

(def element
(Dom/elements
(:xmldoc new-graph)
org.apache.batik.util.XMLConstants/XLINK_NAMESPACE_URI
[:script {"xlink:href" "myscript.js" :type "text/ecmascript"}]
))

(Dom/append-child (Dom/document-element (:xmldoc new-graph)) element)

(Dom/spit-str (:xmldoc new-graph) :indent "yes")
)

Note that I needed to use xlink:href instead of src for embedding javascript refs in svg. src works in HTML not in SVG

Clojure import/require/use defrecord from another namespace

Good GOD!!
Mixing Lisp and Java leads to weirdness of infinite magnitude when I just spent 1 hour trying to figure out wtf was happening with a compilation error. Here’s the situation.
– I created a package called building_blocks (directory building_blocks).
– Namespace called building-blocks.activity (activity.clj file is under building_blocks directory)
– I have a defrecord called Activity inside the building-blocks.activity namespace
– I wanted to use the above mentioned Activity defrecord inside another namespace called adder.core

I continuously got “java.lang.ClassNotFoundException”, “exception in thread “main” java.lang.IllegalArgumentException: Unable to resolve classname: Activity, compiling:(adder/core.clj ” and a bunch of other errors.

As it turns out there were two errors:
– Importing defrecord from another namespace is not just :use. I had to first :require the namespace, then import the defrecord. This was a trivial problem to solve and I figured it out quickly. Only this did not work in my case
– Dashes “-” and Underscores “_” are a nuisance since we are mixing Lisp with Java. While the file system uses underscores Clojure converts things to dashes. Brilliant.

So to fix the second error I use the follow in the ns block

(ns adder.core
(:require building-blocks.activity)
(:import [building_blocks.activity Activity])
)

then I do
(def new-activity (Activity. name))

North Carolina and Tesla – the need for common sense

North Carolina has plans on banning direct sales of Tesla motors’ cars because Tesla does not have deals with dealerships in the state. This law has in many ways been labeled as “unfair competition”, “protecting customers” and “customer service first”. Words and phrases aside what are the pillars on which the above phrases stand?

- Unfair competition:

 Sure Tesla selling directly to customers by the click of a button on the internet is unfair competition. Read it as unfair competition to dealers whose sole purpose is to be middle men between a company and customers. They rare add any value, always seek out best ways to maximise their revenue and profits and never really care about the sold cars. When I tried to purchase a car I was always pulled into various situations that I can phrase as “unfair”. Trying to add unnecessary fees, add ons and asking me to go for financing and then ultimately asking me to purchase a new car – all these have happened in incremental fashion at various dealerships I had visited. To that extent I ended up going to dealerships, sitting around for 3-4 hrs and then returning without a car because the initial price and the final price were quite different.

 How does Tesla sell? You book your car online just the way you purchase electronics. Any issues you have get resolved over phone with customer service or you bring it into the service centre and they service the car. Towing is not that different from the usual cars. How often do you actually have a service centre right around the corner from where your car broke down?

 Where is unfair competition in this segment? Here’s where it is. The only remaining cost optimisation without hurting the customer is reducing the number of layers before the car gets to the customer. i.e; the dealerships getting eliminated. Car companies do not compete with dealerships. There is no real need for dealerships to exist in the first place. Tesla competes with dealerships in the same way Apple competes with Fry’s.

 Labeling it unfair competition would be like Bose labeling Microsoft competition because they share the same floor space at Fry’s. I had a chance to visit two of the Tesla showrooms and I must say that it is one of the nicest places to be. No one pressures me to buy the car. How would you like that taken away?

- Protecting Customers:

 I can only read that as mosquitoes petitioning the govt to protect humans to protect customers. If a customer no longer wants to be a customer they are not customers. This statement assumes that the dealers want to protect the customers. Read it as dealers trying to protect their revenue sources by eliminating competition through lobbying. Try putting this to test by asking customers. Close your shops for 1 week and see how customers react. Try comparing Tesla sales to those of dealership sales for similar category cars. Oh you can actually – Tesla outsold Audi, BMW and Merc in the first quarter of 2013. Now whether this is sustainable over the year is not known. If this is just a flash in the pan then Tesla deserves to die. In which case exactly what is this legislation trying to solve?

 If Tesla does not provide good service to its customers it will be restricted to existing sales and the word of mouth will kill it. Tesla will get sued if it does not fulfill its obligations to the customers. Does it look like USA does not have enough lawyers? Does anyone thing Tesla does not figure that into their business plans?

 Customers can be protected in a multitude of ways without actually forcing the dealership model. I would love to see those laws enacted with fairness. I’d love to see laws that actually protect the customer – let us start with the outrageous EULAs.

- Customer service first:

 Tesla, if it wants to survive, has to put the customer first. Every Tesla owner I’ve talked to has only good things to say about the car. Ofcourse Tesla is small fry in the car market and it remains to be seen if they can sustain this level of engagement but here’s the thing – the worst they can get to is the existing car manufacturers’ status. If that’s the case Tesla’s high price point will kill it anyway. We’ve seen what happened to Microsoft’s Windows 8 when they did not put out a compelling product for customers. Customers know what they don’t want. They don’t necessarily know what they want. No one out there would want bad customer service. Given a choice they’d flock to better service. That’s exactly what I did when AT&T was not sensible about its service. I switched the carrier and went to a smaller one.

 If you have to legislate compulsory customer service go for legislating the car company to customer relationship across the board. It makes no sense to mandate a dealership because the dealers only act as middlemen.

 

What will happen if dealers don’t exist?

 The first thing that will happen is Tesla’s profits will go up and perhaps the costs will come down, but on a bit because the car itself costs >$60K. The problem, if at all, would be that there will be few service centres available to service the cars. Sure that will put off some people and Tesla has to solve this problem before it steps out of being a lifestyle company. At the moment Tesla is rather small to be able to plan this well and on such a massive scale. When it grows it will be forced to expand the service centres if it wants to go solo.

 Tesla needs to have feet on the ground instead of just call centres. Towing will be the insurance company’s responsibility anyway so that is not an issue. Tesla doesn’t really have the financial tie ups so fewer people will be able to avail credit for purchasing the car. Obviously leasing options will help but still this is limited. This is where the dealerships have an edge.

 Dealerships have feet on the ground and are generally one node away from customers. Tesla has a disadvantage here so if dealers can ride this wave they have a point to prove. If they can help customers with:

* Leasing options

* Finance options

* In person experience better than Tesla

the dealers will win the battle. That would send a message to Tesla. Then again am I asking the dealers to evolve? To mutate? To fundamentally stop being leaches?

What should Tesla focus on?

 Here’s my take on the problems that Tesla needs to solve to get to the next level.

 * It needs to increase the charging stations but I get the feeling that this is only temporary fix to the fundamental problem of instant range boost (aka pump some petrol)

* The bigger and better problem to solve is storage capacity. Extending the range from the current 200-300 miles per charge to 500-600 miles would make this an appealing car to the masses

* Not all the problems need to be solved through battery and materials breakthrough. One example is to understand that the car is a transportation medium. As such it should be able to understand what its next task would be. For example if the car only has a 100 Mile range, drive back and forth between work and home is not an issue. Having a means (integrating with travel plans on the phone for example) to find out the next task will help better schedule charges and thus reduce irritations due to sudden death (aka zero charge) mid way between Los Angeles and San Diego.

* Present the economic viability of the car. Tesla has already done a brilliant job at showing some of the economic savings of using the Model S but that is for the filthy rich who use 100 dollar bills instead of tissue paper

* The obvious point of reducing the price to below the $25K mark

* Please do not compromise on the quality. I don’t like driving in the Honda Civic to San Diego because of the horrendous road noise. I prefer my Audi A6 (yes I own an Audi)

* Redefine the car as a lifestyle product and thus differentiate from other cars. The missing piece of the car spectrum is the lifestyle product that allows you to perform tasks/duties/have fun. Tesla has the unique capability to fulfill all these roles if only the massive screen centres around the driver. A GPS takes you from A to B but how about understanding the routine? How about suggesting groceries and planning for the best route? How about planning for picking up the kids? How about posting for a battery charge for the coming weekend? How about telling the user that driving from LA to SF is a risky proposition but here are ways you can reduce it (charging stations, preemptive charges, interesting locations enroute as distractions)? In every case the car understands the context and not just the route.

Disclosure:

 I have NOT purchased a Tesla and for my income, my circumstances and my mindset I consider it too expensive and extravagant. I do love the ride of the car, the amount of space it offers, the next to zero road noise and zero engine noise. In my opinion after the Electronic Fuel Injection the next major step in automotive industry is Tesla Model S (not the Roadster). Come to think of it, the car has not fundamentally changed since the first combustion engine got tossed into it. I am fighting for Tesla purely because it has the potential to change the car industry that has been stagnant and content with reaping the benefits of inertia.

 Tesla also runs purely on electricity and that has huge implications. It is easier to optimise electricity generation and transmission than to change how we consume fossil fuels. Switching out the source of electricity from dirty coal to pseudo clean nuclear to clean solar/wind/tidal sources is a lot easier and transparent to the end user. Heck it is also scalable since various countries can mix and match the various sources of electricity they can produce. Nordic region can use the geo thermal energy while countries close to the equator can go for solar power. Electricity is the most atomic form of energy.

 If we cannot realise this and sweep everything out due to politics and leeches that plague our world, we as humans will be presented only one choice eventually – extermination. Tesla is one of the few instances where economics and common sense converge to form a cool product that can’t be labeled hippie. Who in their right mind would want to douse that flame? Apparently some folks in NC do.

Remote access to mysql

I have a Mysql daemon running on a my Ubuntu system and I was trying to remotely access it from my development machine. That is where hell broke loose because I just couldn’t. I read a lot of articles and stackoverflow articles with no proper resolution. I thought that it is important for me to post on my blog because I am certain this will turn up on google at a later stage when 2 yrs down the road I face the same problem.
I tried changing iptables, mysql bind address etc but none of them worked. So what worked?

 

# Edit /etc/mysql/my.cnf

Added bind-address as the dev machine’s ip address (or add % if you want to open it up to all ip addresses, though won’t suggest this)

# Add a new user accessible over the network

I used mysql workbench for this. In the administration pane, I added the Ubuntu (mysql) server, created a new user and gave the required access levels. Then I added my dev machine’s IP address as accessible sources

# Restart mysql

sudo service mysql restart

This worked. Other things did not work.