ak-lab-4/hello1.c

126 lines
3.5 KiB
C
Raw Permalink Normal View History

2024-11-12 18:00:14 +02:00
/*
* Copyright (c) 2017, GlobalLogic Ukraine LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the GlobalLogic.
* 4. Neither the name of the GlobalLogic nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY GLOBALLOGIC UKRAINE LLC ``AS IS`` AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GLOBALLOGIC UKRAINE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/slab.h>
MODULE_AUTHOR("Serhii Popovych <serhii.popovych@globallogic.com>");
MODULE_DESCRIPTION("Hello, world in Linux Kernel Training");
MODULE_LICENSE("Dual BSD/GPL");
struct TimingList {
struct TimingList *next;
ktime_t t_before;
ktime_t t_after;
2024-11-12 18:00:14 +02:00
};
static struct TimingList *tl_head;
static struct TimingList* write_first_node(void)
2024-11-12 18:00:14 +02:00
{
tl_head = kmalloc(sizeof(struct TimingList), GFP_KERNEL);
tl_head->next = NULL;
return tl_head;
2024-11-12 18:00:14 +02:00
}
static struct TimingList* insert_next_node(struct TimingList *tail)
2024-11-12 18:00:14 +02:00
{
struct TimingList *new_tl_node = kmalloc(sizeof(struct TimingList),
GFP_KERNEL);
tail->next = new_tl_node;
2024-11-12 18:00:14 +02:00
new_tl_node->next = NULL;
return new_tl_node;
}
static struct TimingList* add_new_node(struct TimingList *tail)
{
if (tail == NULL)
return write_first_node();
else
return insert_next_node(tail);
2024-11-12 18:00:14 +02:00
}
static void print_time_diff(struct TimingList *node)
2024-11-12 18:00:14 +02:00
{
2024-12-01 19:06:10 +02:00
pr_info("%lld\n", node->t_after - node->t_before);
2024-11-12 18:00:14 +02:00
}
void destroy_list(void)
2024-11-12 18:00:14 +02:00
{
struct TimingList *curr_tl = tl_head;
struct TimingList *next_tl = NULL;
tl_head = NULL;
2024-11-12 18:00:14 +02:00
while (curr_tl != NULL) {
print_time_diff(curr_tl);
2024-11-12 18:00:14 +02:00
next_tl = curr_tl->next;
kfree(curr_tl);
curr_tl = next_tl;
}
}
static int __init hello_init(void)
{
return 0;
}
void print_hello(int msg_count)
{
int i;
struct TimingList *current_tail = tl_head;
2024-11-12 18:00:14 +02:00
for (i = 0; i < msg_count; i++) {
current_tail = add_new_node(current_tail);
2024-11-12 18:00:14 +02:00
current_tail->t_before = ktime_get();
2024-12-01 19:06:10 +02:00
pr_info("Hello, world!\n");
current_tail->t_after = ktime_get();
2024-11-12 18:00:14 +02:00
}
}
static void __exit hello_exit(void)
{
destroy_list();
}
EXPORT_SYMBOL(print_hello);
EXPORT_SYMBOL(destroy_list);
2024-11-12 18:00:14 +02:00
module_init(hello_init);
module_exit(hello_exit);