Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Linux Kernel Keystroke Counter
View unanswered posts
View posts from last 24 hours

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
zojas
Veteran
Veteran


Joined: 22 Apr 2002
Posts: 1138
Location: Phoenix, AZ

PostPosted: Fri Jul 26, 2002 4:03 pm    Post subject: Linux Kernel Keystroke Counter Reply with quote

I hacked the keyboard driver in the kernel so it counts every keystroke and reports it through /proc/keystrokes.

You can go to http://www.desertsol.com/~kevin/keystroke_patch
to get a patch for 2.4.18 or 2.4.19-gentoo-r7.

Just patch, recompile, install, reboot, then

Code:
cat /proc/keystrokes
Back to top
View user's profile Send private message
delta407
Bodhisattva
Bodhisattva


Joined: 23 Apr 2002
Posts: 2876
Location: Chicago, IL

PostPosted: Fri Jul 26, 2002 4:06 pm    Post subject: Reply with quote

Moving to Tips & Tricks.
_________________
I don't believe in witty sigs.
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 18165

PostPosted: Fri Jul 26, 2002 4:53 pm    Post subject: Reply with quote

I'm curious... is this something akin to 'uptime'?
_________________
Those who know what's best for us must rise and save us from ourselves.
Back to top
View user's profile Send private message
zojas
Veteran
Veteran


Joined: 22 Apr 2002
Posts: 1138
Location: Phoenix, AZ

PostPosted: Fri Jul 26, 2002 5:05 pm    Post subject: Reply with quote

I just thought it would be interesting to see how many keys I hit during the normal course of a day. I found a project to count keystrokes for windows, but not for linux. so I spent about 2 hours hacking the kernel.

on a heavy day, I got over 100,000 keystrokes. typically I get less than that though.

root-tail comes in handy for showing a continuosly updated count in X. (I had to hack root-tail a bit though, because it only update once every 30 seconds; i got it to update every 3 seconds or so).
Back to top
View user's profile Send private message
stig
Apprentice
Apprentice


Joined: 03 Sep 2002
Posts: 289
Location: Bærum, Norway

PostPosted: Sat Jul 17, 2004 7:20 pm    Post subject: Reply with quote

Is something like this available for the 2.6 kernel series?
_________________
Allting har en ende, pølsen den har to.
Back to top
View user's profile Send private message
zojas
Veteran
Veteran


Joined: 22 Apr 2002
Posts: 1138
Location: Phoenix, AZ

PostPosted: Sat Jul 17, 2004 9:28 pm    Post subject: Reply with quote

I never looked in to updating it. I have the patches for 2.4 posted at

http://desertsol.com/~kevin/keystroke_patch/

it might be easy to update, give it a whirl. let me know if you get it working.
_________________
http://www.desertsol.com/~kevin/ppc
Back to top
View user's profile Send private message
centic
n00b
n00b


Joined: 19 Nov 2003
Posts: 33

PostPosted: Sun Jun 12, 2005 6:36 pm    Post subject: Reply with quote

Sounds interesting, anyone got this working on 2.6-kernels? Also how hard will it be to do this as module to load on-demand?

Thx... Dominik.
Back to top
View user's profile Send private message
dsf
n00b
n00b


Joined: 16 Nov 2004
Posts: 21

PostPosted: Mon Jun 13, 2005 12:30 pm    Post subject: Reply with quote

centic wrote:
Sounds interesting, anyone got this working on 2.6-kernels? Also how hard will it be to do this as module to load on-demand?

Thx... Dominik.


I ported it to 2.6 + module (should work ok as built-in, too). :)

I'm still sorting out bugs but it seems to basically work. Now I need to convert from scan code to key code (because you can have up to 6 scan codes transmitted for a single actual 'key press') so that it will increment by 1 every time you hit a key and not by 4 or 6.

I also need to figure out how to unregister the input handler at module unload time -- instant crash if I don't do this. :) (Reason: kernel is still trying to run code that no longer exists after the module is unloaded... so one HAS to unregister cleanly at module unload time!)

Then I should probably be all good. I'll post the heavily commented ~300 line source when debugged.

If zojas is reading this and wondering how I made it work in 2.6 as a module without hacking any existing kernel files -- the answer is simple: define your own input handler. It will then get called every time there's an input event, and within that input handler... look at only key events and ignore everything else. Works great.

I also made the keystroke counter a 64-bit value... should last you millions of trillions of keypresses before it hits the limit. :-) It probably would save an extra clock cycle or two in the top half handler if I made this unsigned 32-bit (4 billion), but no big deal.

Debugging kernel crashes in VMware is heavenly -- doesn't take out my actual workstation, and only takes 10 seconds to fully boot back up into Gentoo virtual machine. :-)
Back to top
View user's profile Send private message
zojas
Veteran
Veteran


Joined: 22 Apr 2002
Posts: 1138
Location: Phoenix, AZ

PostPosted: Mon Jun 13, 2005 12:43 pm    Post subject: Reply with quote

cool! yes I was wondering how you did it as a module. :)
_________________
http://www.desertsol.com/~kevin/ppc
Back to top
View user's profile Send private message
dsf
n00b
n00b


Joined: 16 Nov 2004
Posts: 21

PostPosted: Mon Jun 13, 2005 2:50 pm    Post subject: Reply with quote

Version 1.0 of the 2.6 kernel driver (and modularized) is done.

This module is included in a 11K patch file. This works for gentoo-sources 2.6.11-r9, but probably should work fine for any recent 2.6 kernel.

http://www.globalcrossing.net/~dsf/linux/keystrokes-1.0.patch

To install this patch:

Code:
cd /usr/src/linux
patch -p1 < keystrokes-1.0.patch
make menuconfig
Device Drivers->Character devices->Keystroke Counting
Exit menuconfig and save new kernel configuration
make && make modules_install
modprobe keystrokes
dmesg|tail -n1

(The dmesg will verify it loaded without errors)

To see how many keystrokes you have entered since module loaded:

Code:
cat /proc/keystrokes


If you ever want to manually unload it:

Code:
modprobe -r keystrokes


If you want it to load at boot time, add one line to /etc/modules.autoload.d/kernel-2.6 that says:

Code:
keystrokes


You could build it into the kernel instead of as a module but I recommend module because it'll be easier to unload quickly if something goes wrong. I also have not tested this driver compiled-in-kernel.

Note: you will need to reapply the patch for each new kernel version since this is an unofficial patch.

Bug reports, questions, and comments are welcome.
Back to top
View user's profile Send private message
mirko_3
l33t
l33t


Joined: 02 Nov 2003
Posts: 605
Location: Birreria

PostPosted: Mon Jun 13, 2005 3:35 pm    Post subject: Reply with quote

Cool it works! I noticed that if you hold down ctrl or shift, for example, it starts increasing really fast as if I were pressing it repeatedly; not that it really matters, it doesn't have to be that accurate...
_________________
Non fa male! Non fa male!
Back to top
View user's profile Send private message
zojas
Veteran
Veteran


Joined: 22 Apr 2002
Posts: 1138
Location: Phoenix, AZ

PostPosted: Mon Jun 13, 2005 3:42 pm    Post subject: Reply with quote

the keyboard repeat is kicking in, just like if you hold down the space bar or a normal key. 'kbrate' can be used to change the rate, and how soon the repeat kicks in.
_________________
http://www.desertsol.com/~kevin/ppc
Back to top
View user's profile Send private message
mirko_3
l33t
l33t


Joined: 02 Nov 2003
Posts: 605
Location: Birreria

PostPosted: Mon Jun 13, 2005 3:51 pm    Post subject: Reply with quote

Yeah, I know that, but what I meant was, is there no way around it? As I said, not that it really matters...
_________________
Non fa male! Non fa male!
Back to top
View user's profile Send private message
mirko_3
l33t
l33t


Joined: 02 Nov 2003
Posts: 605
Location: Birreria

PostPosted: Mon Jun 13, 2005 7:02 pm    Post subject: Reply with quote

Also, anyone have a sed one-liner handy to remove the leading zeros? I'll search the manpage as soon as I get home, if noone has replied by then...
EDIT: this is what I've come up with, in case any other sed-illiterate needs it:
Code:

cat /proc/keystrokes|sed 's/^0*0//'

I suppose there may be prettier ways...
_________________
Non fa male! Non fa male!
Back to top
View user's profile Send private message
dsf
n00b
n00b


Joined: 16 Nov 2004
Posts: 21

PostPosted: Tue Jun 14, 2005 12:21 am    Post subject: Reply with quote

mirko_3 wrote:
Also, anyone have a sed one-liner handy to remove the leading zeros? I'll search the manpage as soon as I get home, if noone has replied by then...
EDIT: this is what I've come up with, in case any other sed-illiterate needs it:
Code:

cat /proc/keystrokes|sed 's/^0*0//'

I suppose there may be prettier ways...


That certainly works. Or you can edit /usr/src/linux/drivers/char/keystrokes.c and change from:
Code:
written = sprintf( buffer, "%016lld\n", keystrokes );

To:
Code:
written = sprintf( buffer, "%lld\n", keystrokes );

Then recompile and reinstall by:
Code:
# cd /usr/src/linux
# make && make modules_install
# modprobe -r keystrokes
# modprobe keystrokes
Back to top
View user's profile Send private message
dsf
n00b
n00b


Joined: 16 Nov 2004
Posts: 21

PostPosted: Tue Jun 14, 2005 12:38 am    Post subject: Reply with quote

I wanted to pass along criticism of the kernel module that I did came from a friend (and I do immensely appreciate him bringing it up; no offense was taken at all).

Why? He's got good points. This kernel module generally works fine, but wouldn't have had been his preferred approach. Reason? /dev/input/event* is readable from user space and is easy for simple scripted or compiled tools to read from them. This is preferrable to the kernel stuff because you have far fewer restrictions (interrupt handling vs non-interrupt, having to protect data structures with mutexes, etc) and a goof in dealing with reading it in user space won't lead to a kernel panic or kernel oops. Also don't have to repatch the kernel every time it gets updated to a newer version.

With that said, I thought it was a fun way of spending a few hours this morning, so I don't have any regrets. :) Besides, my ulterior motive was to blow the dust and cobwebs off my memory of the 2.6 kernel changes. ;)

It also doesn't make a bad template for future kernel modules, too. (With one major exception: most kernel modules would have a top half and bottom half handler. This one was so simple and quick that I left out a BH handler.)

And this is NOT a criticism of the original poster at all. This is still a pretty cool idea! It now better explains why the letters on my keyboard is wearing off. :-)
Back to top
View user's profile Send private message
dsf
n00b
n00b


Joined: 16 Nov 2004
Posts: 21

PostPosted: Tue Jun 14, 2005 2:16 am    Post subject: Reply with quote

mirko_3 wrote:
Yeah, I know that, but what I meant was, is there no way around it? As I said, not that it really matters...


I think this updated file will do the trick. I haven't tested all corner cases but it seems to work for the common case.

I've also taken out the zero padding, too.


If you've already previously applied the original v1.0 patch, just fetch the updated source file and rebuild.

http://www.globalcrossing.net/~dsf/linux/keystrokes.c

...and throw that file in /usr/src/linux/drivers/char/keystrokes.c then rebuild.

I've also updated the patch for 'new installs' at:

http://www.globalcrossing.net/~dsf/linux/keystrokes-1.0.1.patch
Back to top
View user's profile Send private message
robbyjo
Guru
Guru


Joined: 06 Apr 2003
Posts: 462

PostPosted: Tue Jun 14, 2005 2:26 am    Post subject: Reply with quote

Wow thanks. This is sort of a motivation for me to hack my own kernel module. :D I'll take this as an example. Again, thanks a lot.
Back to top
View user's profile Send private message
gunkyman
n00b
n00b


Joined: 02 Jun 2004
Posts: 19

PostPosted: Fri Jun 17, 2005 4:35 am    Post subject: Reply with quote

Wow this is awesome! A nice addition to my torsmo ... now I realize I use my computer WAY too much T_T
Back to top
View user's profile Send private message
centic
n00b
n00b


Joined: 19 Nov 2003
Posts: 33

PostPosted: Sat Jun 18, 2005 7:25 am    Post subject: Reply with quote

I really like the statement about the size of the counter in the patch:

Code:

+/*
+ * We chose an unsigned 64-bit type so that we don't end up limiting
+ * really, really power users that might hit the keyboard heavily and
+ * use their super-ultra-stable Linux system for years on end. :-)
+ *
+ * Note: that's slightly tongue-in-cheek as 2 to the 64th power is
+ * approximately, oh, 18.4 million trillion. It's more likely the
+ * keyboard will break long before, and system retired, too. :-)
+ */
static u_int64_t keystrokes=0;


I did a quick calculation assuming a user types 100 keys per second, which is a good rate of a typeist, especially for a prolonged amount of time.

This would lead to an overflow after (2^64)/100/60/24/365 years, which is something around 3.5E+11 years! So no need to worry, just happiliy type away, you'll never gonna reach that limit anyway!! Even with a 32-bit unsigned int it would take you more than 81 years to reach an overflow, already quite hard to do!

Dominik.
Back to top
View user's profile Send private message
mirko_3
l33t
l33t


Joined: 02 Nov 2003
Posts: 605
Location: Birreria

PostPosted: Sat Jun 18, 2005 9:18 am    Post subject: Reply with quote

Hmmm... I see you've never seen me typing... 8)

centic wrote:
I really like the statement about the size of the counter in the patch:

Code:

+/*
+ * We chose an unsigned 64-bit type so that we don't end up limiting
+ * really, really power users that might hit the keyboard heavily and
+ * use their super-ultra-stable Linux system for years on end. :-)
+ *
+ * Note: that's slightly tongue-in-cheek as 2 to the 64th power is
+ * approximately, oh, 18.4 million trillion. It's more likely the
+ * keyboard will break long before, and system retired, too. :-)
+ */
static u_int64_t keystrokes=0;


I did a quick calculation assuming a user types 100 keys per second, which is a good rate of a typeist, especially for a prolonged amount of time.

This would lead to an overflow after (2^64)/100/60/24/365 years, which is something around 3.5E+11 years! So no need to worry, just happiliy type away, you'll never gonna reach that limit anyway!! Even with a 32-bit unsigned int it would take you more than 81 years to reach an overflow, already quite hard to do!

Dominik.

_________________
Non fa male! Non fa male!
Back to top
View user's profile Send private message
zojas
Veteran
Veteran


Joined: 22 Apr 2002
Posts: 1138
Location: Phoenix, AZ

PostPosted: Wed Jun 22, 2005 9:22 pm    Post subject: Reply with quote

dsf wrote:
mirko_3 wrote:
Yeah, I know that, but what I meant was, is there no way around it? As I said, not that it really matters...


I think this updated file will do the trick. I haven't tested all corner cases but it seems to work for the common case.



I think your keyboard repeat code is too aggressive. if you type 3 'a's in a row, for example, it will only increase the count by 1.
_________________
http://www.desertsol.com/~kevin/ppc
Back to top
View user's profile Send private message
mirko_3
l33t
l33t


Joined: 02 Nov 2003
Posts: 605
Location: Birreria

PostPosted: Wed Jun 22, 2005 9:31 pm    Post subject: Reply with quote

Well, the fix fixed it, so I'm allright now...
_________________
Non fa male! Non fa male!
Back to top
View user's profile Send private message
zojas
Veteran
Veteran


Joined: 22 Apr 2002
Posts: 1138
Location: Phoenix, AZ

PostPosted: Wed Jun 22, 2005 9:40 pm    Post subject: Reply with quote

my point is that if you type a word like 'teeth' the counter will only increment by 4, rather than the expected 5.
_________________
http://www.desertsol.com/~kevin/ppc
Back to top
View user's profile Send private message
mirko_3
l33t
l33t


Joined: 02 Nov 2003
Posts: 605
Location: Birreria

PostPosted: Wed Jun 22, 2005 9:51 pm    Post subject: Reply with quote

You're right, I just tested it...
_________________
Non fa male! Non fa male!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum