上一節中的proc實現對於開關檔案,控制檔案,以及顯示很少資訊的檔案來說還是比較簡單的,但是對於需要輸出大量資訊像meminfo,或者結構化的資訊像cpuinfo等時就會顯得很笨拙,並且代碼也很不好理解與維護。核心為了簡化這種proc檔案的實現提供了另外一種方案----seq_file介面。
set_file 介面假定你在建立一個虛擬檔案, 它涉及一系列的必須返回給使用者空間的項. 為使用 seq_file, 你必須建立一個簡單的 "iterator" 對象, 它能在序列裡建立一個位置(start), 向前進(next), 並且輸出序列裡的一個項(show). 它可能聽起來複雜,但實際上過程非常簡單.
seq_file介面即可以實現以前的非結構化的資訊顯示,也可以實現結構化的資訊顯示,並且兩種實現的代碼都比較簡單清晰。使用seq_file介面需要包含標頭檔<linux/seq_file.h>。
seq_file的使用比較規範,與其他檔案操作步驟比較一致,所以看起來也比較容易,我們可以通過分析核心中對meminfo和cpuinfo來學習非結構化與結構化資訊顯示的實現步驟。 1,結構化資訊顯示方式 第一步:在模組初始化時調用proc_create來建立相應proc檔案
static int __init proc_cpuinfo_init(void){proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);return 0;}
第二步:在模組卸載時調用remove_proc_entry函數刪除相應的proc檔案,當然因為cpuinfo的實現在核心代碼中,不涉及到卸載,所以cpuinfo沒有刪除相應proc檔案的操作,但是我們自己編寫的核心模組涉及此情境,所以需要實現此步驟。
第三步:定義file_operations結構體(第一步中建立時會使用)
static const struct file_operations proc_cpuinfo_operations = {.open= cpuinfo_open,.read= seq_read,.llseek= seq_lseek,.release= seq_release,};
第四步,實現file_operations的open函數,在open函數中建議proc檔案與seq_file機制的四個迭代器的聯絡。而read、llseek、release等都是seq_file實現好的架構,不需要另外實現。
extern const struct seq_operations cpuinfo_op;static int cpuinfo_open(struct inode *inode, struct file *file){return seq_open(file, &cpuinfo_op);}
第五步,定義seq_operations結構體
const struct seq_operations cpuinfo_op = {.start= c_start,.next= c_next,.stop= c_stop,.show= show_cpuinfo,};
第六步,實現迭代器(start/next/stop/show),按自己需要來實現,這裡只是cpuinfo的實現
start迭代器:
static void *c_start(struct seq_file *m, loff_t *pos){*pos = cpumask_next(*pos - 1, cpu_online_mask);if ((*pos) < nr_cpu_ids)return &cpu_data(*pos);return NULL;}
next迭代器:
static void *c_next(struct seq_file *m, void *v, loff_t *pos){(*pos)++;return c_start(m, pos);}
stop迭代器,一般實現為空白
static void c_stop(struct seq_file *m, void *v){}
show迭代器,最主要的資訊顯示
static int show_cpuinfo(struct seq_file *m, void *v){struct cpuinfo_x86 *c = v;unsigned int cpu;int i;cpu = c->cpu_index;seq_printf(m, "processor\t: %u\n" "vendor_id\t: %s\n" "cpu family\t: %d\n" "model\t\t: %u\n" "model name\t: %s\n", cpu, c->x86_vendor_id[0] ? c->x86_vendor_id : "unknown", c->x86, c->x86_model, c->x86_model_id[0] ? c->x86_model_id : "unknown");if (c->x86_mask || c->cpuid_level >= 0)seq_printf(m, "stepping\t: %d\n", c->x86_mask);elseseq_printf(m, "stepping\t: unknown\n");if (c->microcode)seq_printf(m, "microcode\t: 0x%x\n", c->microcode);if (cpu_has(c, X86_FEATURE_TSC)) {unsigned int freq = cpufreq_quick_get(cpu);if (!freq)freq = cpu_khz;seq_printf(m, "cpu MHz\t\t: %u.%03u\n", freq / 1000, (freq % 1000));}/* Cache size */if (c->x86_cache_size >= 0)seq_printf(m, "cache size\t: %d KB\n", c->x86_cache_size);show_cpuinfo_core(m, c, cpu);show_cpuinfo_misc(m, c);seq_printf(m, "flags\t\t:");for (i = 0; i < 32*NCAPINTS; i++)if (cpu_has(c, i) && x86_cap_flags[i] != NULL)seq_printf(m, " %s", x86_cap_flags[i]);seq_printf(m, "\nbogomips\t: %lu.%02lu\n", c->loops_per_jiffy/(500000/HZ), (c->loops_per_jiffy/(5000/HZ)) % 100);#ifdef CONFIG_X86_64if (c->x86_tlbsize > 0)seq_printf(m, "TLB size\t: %d 4K pages\n", c->x86_tlbsize);#endifseq_printf(m, "clflush size\t: %u\n", c->x86_clflush_size);seq_printf(m, "cache_alignment\t: %d\n", c->x86_cache_alignment);seq_printf(m, "address sizes\t: %u bits physical, %u bits virtual\n", c->x86_phys_bits, c->x86_virt_bits);seq_printf(m, "power management:");for (i = 0; i < 32; i++) {if (c->x86_power & (1 << i)) {if (i < ARRAY_SIZE(x86_power_flags) && x86_power_flags[i])seq_printf(m, "%s%s", x86_power_flags[i][0] ? " " : "", x86_power_flags[i]);elseseq_printf(m, " [%d]", i);}}seq_printf(m, "\n\n");return 0;}
2,非結構化資訊顯示方式 第一步:在模組初始化時調用proc_create來建立相應proc檔案
static int __init proc_meminfo_init(void){proc_create("meminfo", 0, NULL, &meminfo_proc_fops);return 0;}
第二步:在模組卸載時調用remove_proc_entry函數刪除相應的proc檔案,當然因為meminfo的實現在核心代碼中,不涉及到卸載,所以meminfo沒有刪除相應proc檔案的操作,但是我們自己編寫的核心模組涉及此情境,所以需要實現此步驟。
第三步:定義file_operations結構體(第一步中建立時會使用)
static const struct file_operations meminfo_proc_fops = {.open= meminfo_proc_open,.read= seq_read,.llseek= seq_lseek,.release= single_release,};
第四步,實現file_operations的open函數,因為meminfo是非結構化資訊顯示,所以我們不需要實現start/next/stop等迭代器,只需要實現show來顯示資訊即可,所以不需要像cpuinfo一樣需要定義一個seq_operations結構體並且調用seq_open來建立proc檔案與seq_file迭代器之間的聯絡。seq_file介面對於非結構化的資訊顯示情況提供了一個single_open介面,所以在open中調用single_open。
static int meminfo_proc_open(struct inode *inode, struct file *file){return single_open(file, meminfo_proc_show, NULL);}
第五步,只需要實現show迭代器meminfo_proc_show即可
static int meminfo_proc_show(struct seq_file *m, void *v){struct sysinfo i;unsigned long committed;unsigned long allowed;struct vmalloc_info vmi;long cached;unsigned long pages[NR_LRU_LISTS];int lru;/* * display in kilobytes. */#define K(x) ((x) << (PAGE_SHIFT - 10))si_meminfo(&i);si_swapinfo(&i);committed = percpu_counter_read_positive(&vm_committed_as);allowed = ((totalram_pages - hugetlb_total_pages())* sysctl_overcommit_ratio / 100) + total_swap_pages;cached = global_page_state(NR_FILE_PAGES) -total_swapcache_pages - i.bufferram;if (cached < 0)cached = 0;get_vmalloc_info(&vmi);for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)pages[lru] = global_page_state(NR_LRU_BASE + lru);/* * Tagged format, for easy grepping and expansion. */seq_printf(m,"MemTotal: %8lu kB\n""MemFree: %8lu kB\n""Buffers: %8lu kB\n""Cached: %8lu kB\n""SwapCached: %8lu kB\n""Active: %8lu kB\n""Inactive: %8lu kB\n""Active(anon): %8lu kB\n""Inactive(anon): %8lu kB\n""Active(file): %8lu kB\n""Inactive(file): %8lu kB\n""Unevictable: %8lu kB\n""Mlocked: %8lu kB\n"#ifdef CONFIG_HIGHMEM"HighTotal: %8lu kB\n""HighFree: %8lu kB\n""LowTotal: %8lu kB\n""LowFree: %8lu kB\n"#endif#ifndef CONFIG_MMU"MmapCopy: %8lu kB\n"#endif"SwapTotal: %8lu kB\n""SwapFree: %8lu kB\n""Dirty: %8lu kB\n""Writeback: %8lu kB\n""AnonPages: %8lu kB\n""Mapped: %8lu kB\n""Shmem: %8lu kB\n""Slab: %8lu kB\n""SReclaimable: %8lu kB\n""SUnreclaim: %8lu kB\n""KernelStack: %8lu kB\n""PageTables: %8lu kB\n"#ifdef CONFIG_QUICKLIST"Quicklists: %8lu kB\n"#endif"NFS_Unstable: %8lu kB\n""Bounce: %8lu kB\n""WritebackTmp: %8lu kB\n""CommitLimit: %8lu kB\n""Committed_AS: %8lu kB\n""VmallocTotal: %8lu kB\n""VmallocUsed: %8lu kB\n""VmallocChunk: %8lu kB\n"#ifdef CONFIG_MEMORY_FAILURE"HardwareCorrupted: %5lu kB\n"#endif,K(i.totalram),K(i.freeram),K(i.bufferram),K(cached),K(total_swapcache_pages),K(pages[LRU_ACTIVE_ANON] + pages[LRU_ACTIVE_FILE]),K(pages[LRU_INACTIVE_ANON] + pages[LRU_INACTIVE_FILE]),K(pages[LRU_ACTIVE_ANON]),K(pages[LRU_INACTIVE_ANON]),K(pages[LRU_ACTIVE_FILE]),K(pages[LRU_INACTIVE_FILE]),K(pages[LRU_UNEVICTABLE]),K(global_page_state(NR_MLOCK)),#ifdef CONFIG_HIGHMEMK(i.totalhigh),K(i.freehigh),K(i.totalram-i.totalhigh),K(i.freeram-i.freehigh),#endif#ifndef CONFIG_MMUK((unsigned long) atomic_long_read(&mmap_pages_allocated)),#endifK(i.totalswap),K(i.freeswap),K(global_page_state(NR_FILE_DIRTY)),K(global_page_state(NR_WRITEBACK)),K(global_page_state(NR_ANON_PAGES)),K(global_page_state(NR_FILE_MAPPED)),K(global_page_state(NR_SHMEM)),K(global_page_state(NR_SLAB_RECLAIMABLE) +global_page_state(NR_SLAB_UNRECLAIMABLE)),K(global_page_state(NR_SLAB_RECLAIMABLE)),K(global_page_state(NR_SLAB_UNRECLAIMABLE)),global_page_state(NR_KERNEL_STACK) * THREAD_SIZE / 1024,K(global_page_state(NR_PAGETABLE)),#ifdef CONFIG_QUICKLISTK(quicklist_total_size()),#endifK(global_page_state(NR_UNSTABLE_NFS)),K(global_page_state(NR_BOUNCE)),K(global_page_state(NR_WRITEBACK_TEMP)),K(allowed),K(committed),(unsigned long)VMALLOC_TOTAL >> 10,vmi.used >> 10,vmi.largest_chunk >> 10#ifdef CONFIG_MEMORY_FAILURE,atomic_long_read(&mce_bad_pages) << (PAGE_SHIFT - 10)#endif);hugetlb_report_meminfo(m);arch_report_meminfo(m);return 0;#undef K}