Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kyu Ho Park Sept. 22, 2015 1 Lecture 4 proc file system(procfs) (Project 2 included) Sept.19,4pm.

Similar presentations


Presentation on theme: "Kyu Ho Park Sept. 22, 2015 1 Lecture 4 proc file system(procfs) (Project 2 included) Sept.19,4pm."— Presentation transcript:

1 Kyu Ho Park Sept. 22, 2015 1 Lecture 4 proc file system(procfs) (Project 2 included) Sept.19,4pm

2 Computer Engineering Research Laboratory Why do we need the procfs?  To access the various information of the Linux kernel easily, procfs has been provided(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 system calls ?  It is always available and is accessible to all users, of course,with appropriate permissions only. 2

3 Computer Engineering Research Laboratory ls /proc 3

4 Computer Engineering Research Laboratory /proc/cd 1 4

5 Computer Engineering Research Laboratory cat cpuinfo 5

6 Computer Engineering Research Laboratory meminfo 6

7 Computer Engineering Research Laboratory Size of file at /proc directory 7

8 Computer Engineering Research Laboratory Cat /proc/cpuinfo  The contents of files under /proc directory are created dynamically when you give commands such as ‘cat /proc/meminfo’. 8

9 Computer Engineering Research Laboratory 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

10 Computer Engineering Research Laboratory 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:

11 Computer Engineering Research Laboratory 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

12 Computer Engineering Research Laboratory Delete proc_files and directories  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

13 Computer Engineering Research Laboratory 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 typically 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

14 Computer Engineering Research Laboratory write  static int my_write(struct file *file, const char *buffer, unsigned 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

15 Computer Engineering Research Laboratory for_each_process( ) at #include int read_process(char *buf, char **start, off_t offset,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");

16 Computer Engineering Research Laboratory cat /proc/proc_list 16

17 Computer Engineering Research Laboratory proc_create( ) using seq_file 17

18 Computer Engineering Research Laboratory proc_create() or create_proc_entry() #include If (LINUX_VERSION_CODE )< VERSION(3,11,0)&&defined(CONFIG_PROC_FS) create_proc_entry(); else proc_create( ); 18

19 2015. 09. 24. Project 2. “Linux Fundamental” procfs By Dong Jae Shin

20 Computer Engineering Research Laboratory 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

21 Computer Engineering Research Laboratory 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%

22 Computer Engineering Research Laboratory (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 http://lwn.net/images/pdf/LDD3/ch04.pdf 22 procfs

23 Computer Engineering Research Laboratory (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 https://www.kernel.org/doc/Documentation/filesystems/ proc.txt 23 procfs

24 Computer Engineering Research Laboratory (3)/proc File System  CPU Information  cat /proc/cpuinfo 24  Main Memory Information  cat /proc/meminfo Kernel Debugging

25 Computer Engineering Research Laboratory (4)Create a proc entry  Download Skeleton Files  # wget http://core.kaist.ac.kr/~EE516/Projects/Project2/Makefile http://core.kaist.ac.kr/~EE516/Projects/Project2/Makefile  # wget http://core.kaist.ac.kr/~EE516/Projects/Project2/proc_sam ple.c http://core.kaist.ac.kr/~EE516/Projects/Project2/proc_sam ple.c  Compile  # make  Useful ref)  http://linux.die.net/lkmpg/c708.html http://linux.die.net/lkmpg/c708.html 25 procfs

26 Computer Engineering Research Laboratory (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

27 Computer Engineering Research Laboratory 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

28 Computer Engineering Research Laboratory Task 1  Analyze the given skeleton of promon.c.  Find the functions that you do not know and explain the operations of those functions referring to references and Google. 28

29 Computer Engineering Research Laboratory 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, 3 rd Project Tasks

30 Computer Engineering Research Laboratory task_struct  task_struct (=Process Descriptor) 30  KERNEL/include/linux/sch ed.h  Useful materials : Textbook Chapter 3. of ULK  free e-book version : http://gauss.ececs.uc.edu/ Courses/c4022/code/mem ory/understanding.pdf http://gauss.ececs.uc.edu/ Courses/c4022/code/mem ory/understanding.pdf Project Tasks

31 Computer Engineering Research Laboratory task_struct 31 ………

32 Computer Engineering Research Laboratory 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 VIRT Project Tasks

33 Computer Engineering Research Laboratory 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_bytesread_bytes Disk Cache in DRAM Process wcharrchar Project Tasks

34 Computer Engineering Research Laboratory 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

35 Computer Engineering Research Laboratory Example of Output 35 5. Project Tasks

36 Computer Engineering Research Laboratory 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

37 Computer Engineering Research Laboratory 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 figures)  Submission  Due Date : Oct. 8, PM 23:59  Delay Penalty  10%/day (AM 00:00)  E-mail : djshin@core.kaist.ac.krdjshin@core.kaist.ac.kr  [EE516 Project2] student_number.zip  (various compression format is allowed)


Download ppt "Kyu Ho Park Sept. 22, 2015 1 Lecture 4 proc file system(procfs) (Project 2 included) Sept.19,4pm."

Similar presentations


Ads by Google