A difficult bug to trace..!

Posted: 18th June 2012 by Ammar Qammaz in Post
Tags: ,

During the last 2 weeks some major things happened in Greece including the second round of elections etc.
I spent most of the time stuck and trying to trace a bug in RemoteVariables ( which I am currently developing )..

For testing the under construction library I wrote a test suite which performed 100 variable changes from peer to peer and finally exited when all of the handshakes where complete..
The test suite was then debugged using valgrind and standard gdb and despite some early mixups and architecture problems that were very evident with the debugger output after they were gradually fixed some very strange behaviour ensued as the test suite would “randomly” stop at some point and the program “seemed” to stop acknowledging and transmitting changes on the variables without an apparent reason..

Adding to the complexity of identifying the problem was the fact that a total of 6 threads spanning in two procedures could potentially cause a deadlock somewhere ( despite the mutex protection ) ..

After 5 or 6 days of trying to fix the problem I started to get desperate going line by line and almost rewriting all the critical segments until I figured it out..

The problematic code was the following :

unsigned long GetVariableHash(struct VariableShare * vsh,void * ptr,unsigned int size_of_ptr)
{
if (size_of_ptr unsigned long * stacklongptr = ptr;
unsigned long stacklong = *stacklongptr;
return stacklong;
}else
{
return rvhash(ptr,size_of_ptr);
}
return 0;
}

Each pointer (void * ptr) points to a memory block of variable size (unsigned int size_of_ptr ) and the hash values get stored on an unsigned long ..
All is good for variables that are longer than an unsigned long since they are casted to a char * "string" and then a hash function converts them to a reasonably unique unsigned long.. But .. for smaller sizes of pointers such as unsigned ints my code directly casted the 4byte payload of the pointer to the 8byte accomodation of the unsigned long leaving 4 bytes uninitialized..

What was very funny was that in all the fprintf(stderr,"hash %u",(unsigned int) hash); calls I had added to review the function the results appeared completely normal but when two hashes where compared they failed the equal if operation even if they seemed to carry the same value when fprintf'ed

To better illustrate a simplified version , what I saw was an instance of

fprintf(stderr,"old hash %u , new hash %u\n",(unsigned int) oldhash,(unsigned int) newhash);
if (oldhash==newhash) { fprintf(stderr,"They are the same\n"); } else
{ fprintf(stderr,"They are not the same\n"); }

where I got output like :
old hash 5 , new hash 5
They are not the same

Needless to say valgrind ( which is a great tool and this wasnt its fault :) ) has an initialization check that did not report the unitialized bytes , the possible problem of unitialized bytes didnt even cross my mind while hastly writting the function @ 3 AM 2 years ago ( the problematic commit ) and 2 weeks of my life were "kind of" thrown away ..

It is true that you can sometimes shoot yourself in the foot when using tools that give you this kind of freedom , and this might be just such a case , but I frankly found it enlighting..
C is an awesome language!

The fix commit

Old games , new games , tablets and Linux ramble..

Posted: 3rd June 2012 by Ammar Qammaz in Post
Tags:

I am a relatively young but both “old” gamer :)
The first games I ever played on my first computer ( a Windows 95 PC ) where shareware , like SinkSub , Microman , Comet Busters , Zeek the Geek ..
Needless to say while they were simple both from gameplay and graphics perspectives they were also advanced at the time due to the fact that instead of beeing written for ( the standard back then ) DOS platform they went a step beyond using windows controls and sprites rendered on windows..
Back then having only slow 28.8Kbps internet connections you could buy a CD with a 1000 shareware games inside , and you could actually try almost all of them to find the ones that you liked the most ( probably 100 out of a 1000 ) ..
To finance their projects the shareware game makers relied to friend to friend sharing of the game and registrations that unlocked the full version of the game from people that found it interesting ..
“Serious , large budget games” however like The Lost tales of Atlantis , X-Com Apocalypse, Duke Nukem 3D ( the list goes on and on :P ) , where still written for DOS which offered compatibility and a larger target group until DirectX and OpenGL came along and everything shifted onwards ..

Fast forward to today I believe I can see the same thing happening all over again.. :)
The “Windows 95″ of the time is now being replaced with Tablets ( iOS , Android , or Win8 [ in the near future ] ) and regular PC gaming is starting to decline much like DOS did.. What is different though is that the internet makes the “new sharewhare” ad-ware and that the 1000 mini games for the computers of 1995 are now 500.000 for Google Play .. Last but not least console gaming which was at its infancy back then is now a viable gaming industry on its own so I dont expect it to stop because of the “disruption” of tablet gaming.. Maybe NintendoDS and PSVita are the only platforms bound to suffer from these developments..

Linux on the other hand is a zen-like figure since it is very compatible with older games both using dosbox as well as wine and emulators for other platforms and lately from what I hear also “merging” android and dekstop linux

No one knows what the future holds but the achilles’s heel of the new-age App markets is that their success can (if not organized properly) drown new developers under a sea of other mediocre applications ..
The main problem as I see it can be described with the following real-life scenario (that is about to happen..) ..
Frédérick Raynal of Adeline software ( LBA , Time Commando ) , is now writing an Iphone game called BOxOn .. It will be good , no doubt about that..
How on earth and how long will it take for a good game coming from a great developer to possibly stand out in a croud of 500.000 other applications..!?
The numbers are huge..

A secondary problem is that the new platforms are purely consumer oriented.. meaning that a young kid that finds angry birds “inspiring” will not be able to try and make his own game on his tablet.. In contrast with my experience.. :)
This is also bad but I guess anyone that has the curiosity will always find a way..!!

RemoteVariables!

Posted: 30th May 2012 by Ammar Qammaz in Post
Tags:

During the last weeks I finally found time to work on one of my older ideas ( 2 years old :P ) about a networking library that wouldn’t expose any network calls to the programmer..!

From my experience writing small games and web server services network connectivity using sockets is very nice and efficient indeed , but it has a lot of programming overhead to basically do simple things like passing values or strings from one host to the other..

With RemoteVariables what I am doing is writing a framework that takes care of all the protocol issues and then assumes the role of synchronizing specific elements of the address space of the first computer to the second one..!

So instead of recv/send ing , worrying about half read messages or making a new mini wrapper protocol for every new application the idea goes like this ..

/* CLIENT CODE */
struct VariableShare * vsh = ConnectToRemote_VariableSharing(“SHARE2″,”127.0.0.1″,12345,”password”);
static volatile int SHARED_VAR=0; Add_VariableToSharingList(vsh,”SHARED_VAR”,7,&SHARED_VAR,sizeof(SHARED_VAR));

/* SERVER CODE*/
struct VariableShare * vsh = Start_VariableSharing(“SHARE2″,”127.0.0.1″,12345,”password”);
static volatile int SHARED_VAR=0;
Add_VariableToSharingList(vsh,”SHARED_VAR”,7,&SHARED_VAR,sizeof(SHARED_VAR));

from there on variable SHARED_VAR is “shared” meaning that when it changes on one host we know that the second host will get it.
The change is “sensed” by a second thread monitoring the values of the address space and transmiting them when they change.. It could be the case that the compiler could spit in a Sync(&SHARED_VAR) operation after each change for maximum efficiency .. Some one can also disable automatic “sensing” and perform the Sync call manually to avoid the overheads of the extra thread on the background..

Another example is for lets say a Tic Tac Toe game where we will have
/* CLIENT CODE */
struct VariableShare * vsh = ConnectToRemote_VariableSharing(“TICSHARE”,”127.0.0.1″,12345,”password”);
static volatile int OURMOVE=0,OPPONENTMOVE=0;
Add_VariableToSharingList(vsh,”HOST_MOVE”,7,&OPPONENTMOVE,sizeof(OPPONENTMOVE));
Add_VariableToSharingList(vsh,”SHARED_VAR”,7,&OURMOVE,sizeof(OURMOVE));

/* SERVER CODE */
struct VariableShare * vsh = Start_VariableSharing(“TICSHARE”,”127.0.0.1″,12345,”password”);
static volatile int OURMOVE=0,OPPONENTMOVE=0;
Add_VariableToSharingList(vsh,”SHARED_VAR”,7,&OPPONENTMOVE,sizeof(OPPONENTMOVE));
Add_VariableToSharingList(vsh,”HOST_MOVE”,7,&OURMOVE,sizeof(OURMOVE));

Notice the twisted assignment of the SHARED_VAR , HOST_MOVE ..
This way we could litterally write a network tictac toe program by just checking the OURMOVE , OPPONENTMOVE variables just like writing a two player hot seat game , no more extra code , maybe just for checking if the Client has dropped something that doesnt really happen on hot seat games ..

Needless to say its not over yet but I am making good progress..

The Repository is here ( https://github.com/AmmarkoV/RemoteVariable )..

To test it issue :
git clone git://github.com/AmmarkoV/RemoteVariable.git
./apt-get-dependencies.sh
./make
and you can then test a Score4 Game by issuing
./TestGameMasterAndClone.sh

or test the standalone library via a unit test by issuing
./TestMasterAndClone.sh
of
./MemTestMasterAndClone.sh
for valgrind analysis too..

FOSSCOMM 2012

Posted: 14th May 2012 by Ammar Qammaz in Post
Tags:

Just came back from fosscomm 2012 xD
Interesting topics , great people and lots of fun..
Our group fossaueb was especially adventurous with lots of happy accidents like some of the dudes missing the train stop at Serres , walking in circles trying to find the conference and other things..

Noteable sessions were the ones made by Dimitris Andreadis , one of the team leaders of the JBoss project , the Greek Pirate party speeches and Creative Commons topics , the Unix-like kernel programming from scratch by Giannis Tsiompikas and the workshops including RPM packaging step by step by Nikos Roussos aka comzeradd

I also took a lot of photos some of which are in this photo album tarball :P

A T-ring and a Nikon-D7000 mounted on the telescope

Posted: 3rd May 2012 by Ammar Qammaz in Post
Tags: ,

Today I purchased a T-Adapter made from bresser along with a Nikon adapter for my telescope ( MEADE DS-2102MAK )

The T-Adapter was orignaly meant for the ETX-90/125 but fitted just fine and after some focusing it allowed the camera to produce magnificent quality pictures.. Unfortunately I ran the battery dry after an hour or so while trying different combinations of focusing , ISO settings etc. so I didnt take photos of the moon and other planets I wanted tonight.

The weather also didnt help since there are some scattered clouds that pass and spoil the fun :) ..

I managed to capture a good still of Saturn and I am looking forward to Saturday when the so called “super moon” will be visible ( http://www.youtube.com/watch?v=1MPmdJ6mGQ4 )


Saturn using a Nikon-D7000 on a MEADE DS-2102MAK telescope mounted with the Bresser T-adapter for ETX-90/125

The raw picture , 6.6MB ( it will take some time to download )
Exposure time is 1/500 sec , ISO 12800 ( :P )