Day51 - Timer-Based Pollable Event Driver¶
Objective¶
Extend the Day50 pollable character driver into a timer-driven asynchronous event framework.
Features implemented:
- delayed_work periodic event source
- timer start/stop commands
- manual trigger events
- runtime statistics
- queue overflow tracking
- sysfs runtime status
- epoll integration
Lab 1 - Refactor Event Generation Helper¶
Goal¶
Move event generation into a shared helper function.
Implementation¶
Create shared helper:
Responsibilities:
- generate sequence number
- queue push
- overflow handling
- statistics update
- wait queue wakeup
Lab 2 - Add delayed_work Timer Event Source¶
Goal¶
Generate periodic events inside the driver.
Device Structure¶
Add:
Work Handler¶
Responsibilities:
- generate timer event
- re-schedule delayed_work
Initialization¶
Cleanup¶
Lab 3 - Add Control Commands¶
Goal¶
Control timer behavior from userspace.
Supported Commands¶
| Command | Description |
|---|---|
| start | Start periodic timer |
| stop | Stop periodic timer |
| trigger | Generate one manual event |
| reset_stats | Clear statistics |
| show_stats | Print runtime statistics |
Example¶
Lab 4 - Add Event Source Information¶
Goal¶
Differentiate manual and timer events.
Event Source Enum¶
Event Structure¶
Userspace Output¶
Lab 5 - Add Runtime Statistics¶
Goal¶
Track runtime event behavior.
Statistics Structure¶
struct mypoll_stats {
u32 events_generated;
u32 events_dropped;
u32 events_manual;
u32 events_timer;
};
Overflow Policy¶
When queue is full:
Lab 6 - Add sysfs Runtime Status¶
Goal¶
Expose runtime driver state through sysfs.
sysfs Node¶
sysfs Attribute¶
Example Output¶
event_sequence=83
generated=83
dropped=174
manual_triggered=1
timer_triggered=82
queue_depth=0
timer_running=0
Lab 7 - epoll Integration¶
Goal¶
Verify that timer events work with epoll.
Userspace Flow¶
Result¶
Timer-generated events successfully wake epoll waiters.
Lab 8 - Python Integration Test¶
Goal¶
Automate runtime validation.
Tested Features¶
- manual trigger
- timer events
- timer stop
- epoll wakeup
- queue overflow
- reset statistics
- sysfs stats
Example Output¶
===== Test 5: Queue overflow should increase dropped counter =====
[OK] Overflow observed: dropped=1, queue_depth=16
Final Result¶
The driver evolved from a simple pollable character device into a reusable asynchronous event framework.
Key concepts learned:
- delayed_work scheduling
- asynchronous event flow
- runtime observability
- queue overflow handling
- epoll integration
- sysfs runtime statistics