3.1.7 branch.
[minix.git] / kernel / profile.c
blob121f60533cb9f633249060e7219ae8e6b9fd66e0
1 /*
2 * This file contains several functions and variables used for system
3 * profiling.
5 * Statistical Profiling:
6 * The interrupt handler for profiling clock.
8 * Call Profiling:
9 * The table used for profiling data and a function to get its size.
11 * The function used by kernelspace processes to register the locations
12 * of their control struct and profiling table.
14 * Changes:
15 * 14 Aug, 2006 Created, (Rogier Meurs)
18 #include <minix/config.h>
20 #include <minix/profile.h>
21 #include <minix/portio.h>
22 #include "kernel.h"
23 #include "profile.h"
24 #include "proc.h"
26 #if SPROFILE
28 #include <string.h>
30 /* Function prototype for the profiling clock handler. */
31 FORWARD _PROTOTYPE( int profile_clock_handler, (irq_hook_t *hook) );
33 /* A hook for the profiling clock interrupt handler. */
34 PRIVATE irq_hook_t profile_clock_hook;
36 /*===========================================================================*
37 * init_profile_clock *
38 *===========================================================================*/
39 PUBLIC void init_profile_clock(u32_t freq)
41 int irq;
43 if((irq = arch_init_profile_clock(freq)) >= 0) {
44 /* Register interrupt handler for statistical system profiling. */
45 profile_clock_hook.proc_nr_e = CLOCK;
46 put_irq_handler(&profile_clock_hook, irq, profile_clock_handler);
47 enable_irq(&profile_clock_hook);
51 /*===========================================================================*
52 * profile_clock_stop *
53 *===========================================================================*/
54 PUBLIC void stop_profile_clock()
56 arch_stop_profile_clock();
58 /* Unregister interrupt handler. */
59 disable_irq(&profile_clock_hook);
60 rm_irq_handler(&profile_clock_hook);
63 /*===========================================================================*
64 * profile_clock_handler *
65 *===========================================================================*/
66 PRIVATE int profile_clock_handler(irq_hook_t *hook)
68 /* This executes on every tick of the CMOS timer. */
70 /* Are we profiling, and profiling memory not full? */
71 if (!sprofiling || sprof_info.mem_used == -1) return (1);
73 /* Check if enough memory available before writing sample. */
74 if (sprof_info.mem_used + sizeof(sprof_info) > sprof_mem_size) {
75 sprof_info.mem_used = -1;
76 return(1);
79 /* All is OK */
81 /* Idle process? */
82 if (priv(proc_ptr)->s_proc_nr == IDLE) {
83 sprof_info.idle_samples++;
84 } else
85 /* Runnable system process? */
86 if (priv(proc_ptr)->s_flags & SYS_PROC && proc_is_runnable(proc_ptr)) {
87 /* Note: k_reenter is always 0 here. */
89 /* Store sample (process name and program counter). */
90 data_copy(KERNEL, (vir_bytes) proc_ptr->p_name,
91 sprof_ep, sprof_data_addr_vir + sprof_info.mem_used,
92 strlen(proc_ptr->p_name));
94 data_copy(KERNEL, (vir_bytes) &proc_ptr->p_reg.pc, sprof_ep,
95 (vir_bytes) (sprof_data_addr_vir + sprof_info.mem_used +
96 sizeof(proc_ptr->p_name)),
97 (vir_bytes) sizeof(proc_ptr->p_reg.pc));
99 sprof_info.mem_used += sizeof(sprof_sample);
101 sprof_info.system_samples++;
102 } else {
103 /* User process. */
104 sprof_info.user_samples++;
107 sprof_info.total_samples++;
109 /* Acknowledge interrupt if necessary. */
110 arch_ack_profile_clock();
112 return(1); /* reenable interrupts */
115 #endif /* SPROFILE */
117 #if CPROFILE
119 * The following variables and functions are used by the procentry/
120 * procentry syslib functions when linked with kernelspace processes.
121 * For userspace processes, the same variables and function are defined
122 * elsewhere. This enables different functionality and variable sizes,
123 * which is needed is a few cases.
126 /* A small table is declared for the kernelspace processes. */
127 struct cprof_tbl_s cprof_tbl[CPROF_TABLE_SIZE_KERNEL];
129 /* Function that returns table size. */
130 PUBLIC int profile_get_tbl_size(void)
132 return CPROF_TABLE_SIZE_KERNEL;
135 /* Function that returns on which execution of procentry to announce. */
136 PUBLIC int profile_get_announce(void)
138 return CPROF_ACCOUNCE_KERNEL;
142 * The kernel "announces" its control struct and table locations
143 * to itself through this function.
145 PUBLIC void profile_register(ctl_ptr, tbl_ptr)
146 void *ctl_ptr;
147 void *tbl_ptr;
149 int proc_nr;
150 vir_bytes vir_dst;
151 struct proc *rp;
153 if(cprof_procs_no >= NR_SYS_PROCS)
154 return;
156 /* Store process name, control struct, table locations. */
157 rp = proc_addr(SYSTEM);
159 cprof_proc_info[cprof_procs_no].endpt = rp->p_endpoint;
160 cprof_proc_info[cprof_procs_no].name = rp->p_name;
161 cprof_proc_info[cprof_procs_no].ctl_v = (vir_bytes) ctl_ptr;
162 cprof_proc_info[cprof_procs_no].buf_v = (vir_bytes) tbl_ptr;
164 cprof_procs_no++;
167 #endif