Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/pid_namespace.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <linux/seq_file.h>
#include <linux/seqlock.h>
#include <linux/timer.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/kernel_stat.h>
#include <linux/netdevice.h>
MODULE_LICENSE("GPL");
#define FSHIFT 11 /* nr of bits of precision */
#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
#define LOAD_INT(x) ((x) >> FSHIFT)
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
#define per_cpu(var, cpu) \
(*SHIFT_PERCPU_PTR(&(var), per_cpu_offset(cpu)))
DECLARE_PER_CPU(struct rq, runqueues);
#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
extern int nr_threads;
extern unsigned long avnrun[]; /* Load averages */
/* get_avenrun - get the load average array
@loads: pointer to dest load array
@offset: offset to add
@shift: shift count to shift the result left
*/
extern void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
{
loads[0] = (avenrun[0] + offset) << shift;
loads[1] = (avenrun[1] + offset) << shift;
loads[2] = (avenrun[2] + offset) << shift;
}
/*nr_running:
externally visible scheduler statistics: current number of runnable
threads performed since bootup
*/
extern unsigned long nr_running(void)
{
unsigned long i, sum = 0;
for_each_online_cpu(i)
sum += cpu_rq(i)->nr_running;
return sum;
}
EXPORT_SYMBOL_GPL(nr_running);
/*calcualation of cpu load*/
static int loadavg_proc_show(struct seq_file *m, void *v)
{
//unsigned long avnrun[3];
get_avenrun(avnrun, FIXED_1/200, 0);
seq_printf(m, "%lu.%02lu %lu.%02lu %lu.%02lu /%d %d\n",
LOAD_INT(avnrun[0]), LOAD_FRAC(avnrun[0]),
LOAD_INT(avnrun[1]), LOAD_FRAC(avnrun[1]),
LOAD_INT(avnrun[2]), LOAD_FRAC(avnrun[2]),
nr_threads,
task_active_pid_ns(current)->last_pid);
return 0;
}
/* single_open open the virtual file.when output time comes it will call
loadavg_proc_show function*/
static int loadavg_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, loadavg_proc_show, NULL);
}
/*The operations - read(), llseek(), and release() - are
all implemented by the seq_file code itself. So a virtual file's
file_operations structure */
static const struct file_operations loadavg_proc_fops = {
.open = loadavg_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
/*loading the module*/
static int __init proc_loadavg_init(void)
{
proc_create("loadavg", 0, NULL, &loadavg_proc_fops);
return 0;
}
/*unloading the module*/
static void __exit proc_loadavg_exit(void)
{
remove_proc_entry("loadavg", NULL);
}
module_init(proc_loadavg_init);
module_exit(proc_loadavg_exit);
this is the perfect way of calculating cpuload in kernel mode.if yes i am getting dereferencing pointer to incomplete type error in sum += cpu_rq(i)->nr_running line please tell me the solution.if not tell me how to calculate
That error message means you needed to include the .h file that defines the struct whose member you are accessing.
Between the macros and the incomplete code, I can't guess what struct that is, so I can't guess what .h file it is.
Maybe you know what struct you are accessing or maybe the error message tells you more than you quoted.
Quote:
Originally Posted by veena hosur
its very urget please reply me
Some experts here will resent a post like that enough that they will skip replying, even if they know the answer.
In my opinion, if the question was urgent FOR YOU, then YOU should have put a bit more effort into providing enough information to enable a better answer on the first try (rather than have me say I can't guess which struct you need the definition of included).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.