kyu ho park sept. 22, 2015 1 lecture 4 proc file system(procfs) (project 2 included) sept.19,4pm

Post on 03-Jan-2016

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Kyu Ho ParkSept. 22, 2015

1

Lecture 4proc file system(procfs)

(Project 2 included)

Sept.19,4pm

Computer Engineering Research Labora-tory

Why do we need the procfs? To access the various information of the

Linux kernel easily, procfs has been pro-vided(firstly by Tom J. Killan, 1984 for UNIX on VAX machine).

Why we do not use the conventional file system using open, close, read, write sys-tem calls ?

It is always available and is accessible to all users, of course,with appropriate per-missions only.

2

Computer Engineering Research Labora-tory

ls /proc

3

Computer Engineering Research Labora-tory

/proc/cd 1

4

Computer Engineering Research Labora-tory

cat cpuinfo

5

Computer Engineering Research Labora-tory

meminfo

6

Computer Engineering Research Labora-tory

Size of file at /proc directory

7

Computer Engineering Research Labora-tory

Cat /proc/cpuinfo The contents of files under /proc directory

are created dynamically when you give commands such as

‘cat /proc/meminfo’.

8

Computer Engineering Research Labora-tory

proc file system(procfs) functions

proc_mkdir( ); //make a directory under /proc. create_proc_entry( ); //make a file under /proc. create_proc_read_entry( ); //make a readonly file. remove_proc_entry( ); //remove dirs or files.

--procfs uses [struct proc_dir_entry] defined as follows; struct proc_dir_entry{

.....struct inode_operations *proc_iops;struct file_operations *proc_fops;..struct proc_dir_entry *next, *parent, *subdir;

void *data;read_proc_t *read_proc;write_proc_t *write_proc;atomic_t count;

int deleted;//in case of delete, this flag should be 0, if in use this flag is 1.

};

9

//mostly used

Computer Engineering Research Labora-tory

Making a directory under /proc

struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent);

// name: the directory name to create,//parent: the parent of the directory to create.

parent

struct proc_dir_entry *son;struct proc_dir_entry *grand_son;son=proc_mkdir(“son”,NULL);grand_son=proc_mkdir(“grand_son”, son);

10

son

grand_son

Ex:

Computer Engineering Research Labora-tory

File creation under /proc struct proc_dir_entry *create_proc_entry(const char *name,

mode_t mode,struct proc_dir_entry *parent);

//name: the RW file name to create. mode:file permission. To create read_only file, struct proc_dir_entry *create_proc_read_entry(const char *name,mode_t mode,struct proc_dir_entry *parent);

Ex:[create a ‘test’ file at /proc/test]struct proc_dir_entry *test_proc;test_proc=create_proc_entry(“test”, 0, NULL);

// if mode is set to 0, the default permission is 0444.

11

Computer Engineering Research Labora-tory

Delete proc_files and direc-tories

void remove_proc_entry(const char *name, struct proc_dir_entry *parent);

12

remove_proc_entry(“test”,NULL);remove_proc_entry(“grand_son”, son);remove_proc_entry(“son”,NULL);

test

Computer Engineering Research Labora-tory

read static int my_read(char *page, char **start, off_t off, int

count, int *eof, void *data) {

……//program codes*eof =1 ; when this function is called only one time.return len; //len: length of data read.

}-The memory space used in procfs is a page whose size is typ-ically 4KBytes. The location to save the data.-eof : end-of-file, if eof =1, no read anymore. -start: it is used rarely.-off: current location of the file.-data: not used in read.

13

Computer Engineering Research Labora-tory

write static int my_write(struct file *file, const char *buffer, un-

signed long count, void *data) { char *proc_data;

proc_data=(char *)data;copy_from_user(kernel_data, buffer, count);

…return count; }

-buffer: user space data,-count: written data size,-*data: kernel address to store the data of buffer.

14

Computer Engineering Research Labora-tory

for_each_process( ) at <linux/sched.h>

#include<linux/kernel.h>#include <linux/proc_fs.h>#include<linux/sched.h>#include<linux/module.h>

int read_process(char *buf, char **start, off_t off-set,int count,int *eof,void *data ){int len=0;struct task_struct *task_list;

for_each_process(task_list) {

len += sprintf(buf+len, "\n %s %d\n",task_list->comm,task_list->pid);

} return len;}

15

void create_new_proc_entry(){create_proc_read_entry("proc_list",0,NULL,read_process,NULL);

}

int ps_init (void) { int ret; create_new_proc_entry(); return 0;}

void ps_exit(void) { remove_proc_entry("proc_list",NULL);}module_init(ps_init);module_exit(ps_exit);

MODULE_LICENSE("GPL");

Computer Engineering Research Labora-tory

cat /proc/proc_list

16

Computer Engineering Research Labora-tory

proc_create( ) using seq_file

17

Computer Engineering Research Labora-tory

proc_create() or create_proc_entry()

#include <linux/version.h>If (LINUX_VERSION_CODE )< VERSION(3,11,0)&&defined(CONFIG_PROC_FS)

create_proc_entry();else

proc_create( );

18

2015. 09. 24.

Project 2.“Linux Fundamental”

procfs

By Dong Jae Shin

Computer Engineering Research Labora-tory

Introduction to Projects Project 1: Setting up Environment for Project Project 2: Linux Fundamental(procfs) Project 3: System Call and Synchronization Project 4: File System Project 5: Device Driver for Embedded H/W

Especially, including “BeagleBoard Development”

20

Computer Engineering Research Labora-tory

Contents Follow-on Tasks

1. Linux System Administration 2. File System & Software Management 3. Build your own Linux Kernel

Project Tasks Write a System Monitoring Tools

1. Analyse the given skeleton program (10%) 2. Traverse Process tasklist (20%) 3. per Process Memory Usage (20%) 4. per Process I/O Usage (20%) 5. Process CPU Utilization (20% point Bonus)

Report (30%)------------------------------------------------------ Max. 100%

Computer Engineering Research Labora-tory

(1)Kernel Debugging Print Kernel-level Message

printk Kernel version of printf usage) printk(“%s %d %lu”, str, i, j );

Kernel Log Message tail -f /var/log/kern.log

See also (optional) for advanced debugging http://lwn.net/images/pdf/LDD3/ch04.pdf

22

procfs

Computer Engineering Research Labora-tory

(2)/proc File System /proc

a special file system which displays the present of the system

pseudo and virtual file system which resides in the RAM

provides a method of communication between kernel space and user space

Various information provided by proc https://

www.kernel.org/doc/Documentation/filesystems/proc.txt

23

procfs

Computer Engineering Research Labora-tory

(3)/proc File System CPU Information

cat /proc/cpuinfo

24

Main Memory Infor-mation cat /proc/meminfo

Kernel Debugging

Computer Engineering Research Labora-tory

(4)Create a proc entry Download Skeleton Files

# wget http://core.kaist.ac.kr/~EE516/Projects/Project2/Makefile

# wget http://core.kaist.ac.kr/~EE516/Projects/Project2/proc_sample.c

Compile # make

Useful ref) http://linux.die.net/lkmpg/c708.html

25

procfs

Computer Engineering Research Labora-tory

(5)Test proc entry Kernel Log

# tail -f /var/log/kern.log Insert Module

insmod proc_sample.ko Write Proc File

# echo blahblah > /proc/proc_sample Read Proc File

# cat /proc/proc_sample Remove Module

# rmmod proc_sample

26

procfs

Computer Engineering Research Labora-tory

Project Tasks Make a System Monitoring Tools

System monitoring is an important job especially on the embedded system

Low computing power, Less memory, Limited resources Monitoring tools

top, ps, netstat, gnome-system-monitor

Tasks 1.Analyze the given skeleton procfs program. (10%) 2. Traverse Process tasklist (30%) 3. per Process Memory Usage (30%) 4. per Process I/O Usage (30%) 5. Process CPU Utilization (10% point Bonus Task)

27

Project Tasks

Computer Engineering Research Labora-tory

Task 1 Analyze the given skeleton of promon.c. Find the functions that you do not know

and explain the operations of those func-tions referring to references and Google.

28

Computer Engineering Research Labora-tory

Task2. Traverse Task List Every Linux Tasks are managed by doubly linked list

29

Print every task’s PID and Process Name in your proc file system Textbook Chapter 3. Understanding the Linux

Kernel, 3rd

Project Tasks

Computer Engineering Research Labora-tory

task_struct task_struct (=Process Descriptor)

30

KERNEL/include/linux/sched.h

Useful materials : Textbook Chapter 3. of ULK

free e-book version : http://gauss.ececs.uc.edu/Courses/c4022/code/memory/understanding.pdf

Project Tasks

Computer Engineering Research Labora-tory

task_struct

31

………

Computer Engineering Research Labora-tory

Task3. Memory Usage Memory Mapping

RSS : Resident Set Size

VIRT : Virtual Memory Size

Print every task’s VIRT and RSS Memory Size

32

VIRT RSS

RSSVIRT

Project Tasks

Computer Engineering Research Labora-tory

Task4. Process I/O Stat I/O Stat is stored in struct task_io_accounting

include/linux/task_io_accounting.h

Hint)

# cat /proc/PID/io rchar : read bytes by process wchar : written bytes by process syscr : # of read system calls syscw : # of write system calls read_bytes : read bytes from disk write_bytes : written bytes to disk

33

write_bytes

read_bytes

Disk Cache in DRAM

Process

wcharrchar

Project Tasks

Computer Engineering Research Labora-tory

Task5. CPU Utilization “top” command

shows the CPU utilization per process

34

Challenging Task (Bonus Point 10%) Kernel manages CPU ticks consumed by each

process in struct task_cputime

5. Project Tasks

Computer Engineering Research Labora-tory

Example of Output

35

5. Project Tasks

Computer Engineering Research Labora-tory

Check your results Check the correctness of your results Task 2.

# ps -ef Task 3.

# top -b -n 1 Task 4.

Browser is a good test case (# firefox) Get pid

ps -ef |grep firefox # cat /proc/PID/io

Task 5. # top

Project Tasks

Computer Engineering Research Labora-tory

Submission Contents

Source Code Report

Project Tasks1~5 Key point of your Source Code Final Screenshots Page Limit : about 5 pages for Project Tasks1~5. (except fig-

ures)

Submission Due Date : Oct. 8, PM 23:59 Delay Penalty

10%/day (AM 00:00) E-mail : djshin@core.kaist.ac.kr [EE516 Project2] student_number.zip

(various compression format is allowed)

top related