https://gravatar.com/userimage/16926726/2de44cb9918f08ff7f430244b10bf37b.jpeg?size=256

gowinder's Blog

Thoughts on IT Competition, Inspired by Google Wave

Today I took my wife out for DQ, and then while keeping her company over a bowl of Ajisen ramen I drank some iced lemon water. The result was that the moment I got home I made a beeline for the toilet. Of course I didn’t forget to bring a magazine — we have a pile of back issues at home, so I grabbed a copy of CBN Weekly at random, no idea which issue it was. Flipping through it, being an IT guy, IT is naturally what interests me most, so the article about Google Wave was the first thing I read.

How the Formatted Log Output Actually Works

The low-level function can keep using the old approach, writing strings directly. It gets called by the other log-output functions.

Then write a proxy function that takes an XMLNODE parameter. This function accepts calls from all the other upper-level log-writing functions; the upper-level functions unify their log format, put the information into an XMLNODE, and then the proxy calls the low-level function to write the log.

As for the upper-level functions, the most common kinds of log output are: plain text output (LOGERROR), assertion ASSERT, and exception CATCH. Make these three into three separate functions or macros. In practice, LOGERROR creates an XMLNODE, writes the log type, time, string and other information into the XMLNODE’s attributes, then calls the proxy function.
Then there’s the logging for each feature block. Item-loss logging, for example, needs its own upper-level log function that formats the information into an XMLNODE and hands it to the proxy function to write.

New Logging Functions: Formatted, Level-Controlled Log Output

In the past, several of our systems used a fairly casual approach to log output: whatever the problem was, you just printed it. Timeout logs, error logs, ASSERTs, exceptions — all dumped into one file. That makes it inconvenient to read, and it makes it hard to react to urgent events. There was also no way to control the output level. Some output is only needed in the DEBUG build or the first RELEASE build; once the version stabilises, those logs are no longer needed. Log levels should all be controllable. I’ll split this into two parts:

The Trouble Caused by the Server's Automatic Time Sync Service

Soldiers Sortie had an outage once, and the cause was the sync service pulling the time backwards. That meant the current time could end up earlier than the timestamp of an event already recorded in memory, so subtracting one from the other produced a negative number — and since we were using DWORD, it became an enormous value and corrupted the data.
The fix: first, it’s best to turn off the time sync service on the server; then check the DWORD difference and skip the operation if it’s negative. I think DWORD has to be used with care — I’ve run into trouble on this more than once!

When the Elements of a Priority Queue Are Pointers

Using a priority queue whose elements are pointers, like this:
typedef priority_queue<TimeEvent*, deque <TimeEvent*>, less<deque<TimeEvent*>::value_type> TIME_EVENT_QUEUE;
Compared this way, the result is a comparison of pointer values. If I want to compare some member of TimeEvent, that doesn’t work. After some Googling, there’s an approach: replace less with a functor struct:
struct TimeEventLessComp
{
bool operator () (TimeEvent*& a, TimeEvent*& b) const
{
return a->dwTick > b->dwTick;
};
};
Then change the declaration to:
typedef priority_queue<TimeEvent*, deque <TimeEvent*>, TimeEventLessComp> TIME_EVENT_QUEUE;

Test Results for the Time Service

After a simple test: generate 500,000 timers, then insert the pointers into a list and into a priority_queue respectively, with the priority_queue using a vector container (it can’t use list). First I used list’s own sort function: 516ms, and I ran it several times, always above 500ms and below 550ms. For the priority_queue, on top of the existing 500,000 I did 100,000 insertions, 31ms in total, 0.00031ms per event on average.
The output looks like this:
list sort time:516 ms
priority_queue push 100000 event use 31 ms time, average 0.000310 ms per event
sorted list sort time:516 ms
sorted list trace 0
sorted list trace 1
….
priority_queue trace 0
priority_queue trace 1
….
So the test shows the priority queue is clearly faster than sorting the LIST myself — a great fit for what I need!