Domanda

I want to count views for a web page, while I am using G-WAN and C. I am not really so familiar with them. I am using Intel 64-bit processor. I have searched for the topic and still get no idea, can anyone help me? Thank you.

È stato utile?

Soluzione 2

I want to count views for a web page, while I am using G-WAN and C

Fine, there are several options:

  • a G-WAN servlet
  • a G-WAN connection handler
  • a G-WAN content-type handler

...and your code can use different ways depending on your needs (listed in order of speed and memory efficiency):

  • a global atomic variable attached to a G-WAN persistent pointer
  • a global G-WAN KV store attached to a G-WAN persistent pointer
  • a system shared memory segment (it will survive a server stop)
  • an in-memory or disk-based database (SQLite, etc.)
  • a DB KV store or server (Memcached, Redis, mySQL, etc.)
  • etc.

You can even use a mix of these and use a fast (in-memory) method that is periodically stored to disk.

It really depends on your needs (is the counter tracking one single page? is it fetched 10,000 times per second, 1 million times? etc.).

Altri suggerimenti

I know very little about G-WAN. I've been looking at their web site, and as far as I can tell it runs one thread per CPU; and your C code is loaded once, then executed in the context of a G-WAN thread each time its needed. In this case you should be able to use normal atomics (e.g. a suitable library for atomic operations for C, or inline assembly if you can't find a suitable library for C).

The problem would be storing your counter on disk (e.g. so that it doesn't reset to zero whenever the server is restarted). In theory, you only need to read the previous value once from disk when the web server is started, and write the new value to disk once whenever the server is stopped (or possibly once every 30 minutes or something in case the server crashes). It's not something on the critical path (e.g. not something that has to be stored/retrieved every time a page is served). In practice, I have no idea if G-WAN has any support for this at all (or if something like the atexit() function works).

If G_WAN has no support for it, you might be able to hack your way around it using (e.g.) a global "time when state was last saved" variable protected by some sort of mutex; where (if it's unset) you load the old state from disk and set the global variable, and if it is set you use it to determine if "X minutes" has passed since is was saved last. Of course acquiring a mutex and checking the global variable would have higher overhead than incrementing your counter; but it'd still be a several thousand times faster than (e.g.) using a database engine.

EDIT:

OK, my bad. I read the question too quickly and got a fixation on the HTTP requests, forgetting about the servlets. I thought of a simple hit counter on a page, hence my wondering at the use of an atomic variable.

Please accept my apologies.

Nevertheless, I don't see the point in optimizing a page counter, when serving the page itself will likely consume infinitely more CPU and resources than the read/modify/write of a single variable.

The KV system already provides a locking mechanism. As far as efficiency is concerned, using the kv_do primitive should handle concurrent accesses and get the job done in a neglectible amount of time.

And I rest my case about this atomic variables business.
Now that the multicore architecture imposes its constraints to about any application programmer,atomic variables seem like the latest trendy silver bullet around.
After spending a dozen years or so designing embedded, multitasking software where concurrent accesses and task synchronization is a constant concern, my conclusion is: they are not.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top