/* * spinlock_thread.c * * Author: Raghavendra K T * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include #include #include #include #include #include #include #include unsigned int start, end, diff; static struct task_struct **spintask_pid; static DECLARE_COMPLETION(spintask_exited); static int total_thread_exit = 0; static DEFINE_SPINLOCK(counter_spinlock); #define DEFAULT_NR_THREADS 4 #define DEFAULT_LOOP_COUNT 4000000L static int nr_spinlock_threads = DEFAULT_NR_THREADS; static long loop_count = DEFAULT_LOOP_COUNT; module_param(nr_spinlock_threads, int, S_IRUGO); module_param(loop_count, long, S_IRUGO); static long count = 0; static int a[2][2] = {{2, 5}, {3, 7}}; static int b[2][2] = {{1, 19}, {11, 13}}; static int m[2][2]; static int n[2][2]; static int res[2][2]; static inline void matrix_initialize(int id) { int i, j; for (i=0; i<2; i++) for(j=0; j<2; j++) { m[i][j] = (id + 1) * a[i][j]; n[i][j] = (id + 1) * b[i][j]; } } static inline void matrix_mult(void) { int i, j, k; for (i=0; i<2; i++) for (j=0; j<2; j++) { res[i][j] = 0; for(k=0; k<2; k++) res[i][j] += m[i][k] * n[k][j]; } } static int input_check_thread(void* arg) { int id = (int)arg; long i = loop_count; allow_signal(SIGKILL); #if 0 matrix_initialize(id); matrix_mult(); #endif do { spin_lock(&counter_spinlock); count++; #if 0 if (id%3) matrix_initialize(id); else if (id%3 + 1) matrix_mult(); #endif spin_unlock(&counter_spinlock); } while(i--); spin_lock(&counter_spinlock); total_thread_exit++; spin_unlock(&counter_spinlock); if(total_thread_exit == nr_spinlock_threads) { rdtscl(end); diff = end - start; complete_and_exit(&spintask_exited, 0); } return 0; } static int spinlock_init_module(void) { int i; char name[20]; printk(KERN_INFO "insmod nr_spinlock_threads = %d\n", nr_spinlock_threads); spintask_pid = kzalloc(sizeof(struct task_struct *)* nr_spinlock_threads, GFP_KERNEL); rdtscl(start); for (i=0; i