1 /**
2  * Copyright (C) Mellanox Technologies Ltd. 2001-2015.  ALL RIGHTS RESERVED.
3  *
4  * See file LICENSE for terms.
5  */
6 
7 #ifndef UCM_EVENT_H_
8 #define UCM_EVENT_H_
9 
10 #include <ucm/api/ucm.h>
11 #include <ucm/util/log.h>
12 #include <ucs/datastruct/list.h>
13 #include <ucs/type/status.h>
14 #include <ucs/sys/topo.h>
15 #include <ucs/memory/memory_type.h>
16 
17 #define UCM_NATIVE_EVENT_VM_MAPPED (UCM_EVENT_MMAP  | UCM_EVENT_MREMAP | \
18                                     UCM_EVENT_SHMAT | UCM_EVENT_SBRK)
19 
20 #define UCM_NATIVE_EVENT_VM_UNMAPPED (UCM_EVENT_MMAP   | UCM_EVENT_MUNMAP | \
21                                       UCM_EVENT_MREMAP | UCM_EVENT_SHMDT  | \
22                                       UCM_EVENT_SHMAT  | UCM_EVENT_SBRK   | \
23                                       UCM_EVENT_MADVISE)
24 
25 
26 typedef struct ucm_event_handler {
27     ucs_list_link_t       list;
28     int                   events;
29     int                   priority;
30     ucm_event_callback_t  cb;
31     void                  *arg;
32 } ucm_event_handler_t;
33 
34 
35 typedef struct ucm_event_installer {
36     ucs_status_t          (*install)(int events);
37     void                  (*get_existing_alloc)(ucm_event_handler_t *handler);
38     ucs_status_t          (*get_mem_type_current_device_info)(ucs_sys_bus_id_t *bus_id, ucs_memory_type_t mem_type);
39     ucs_list_link_t       list;
40 } ucm_event_installer_t;
41 
42 extern ucs_list_link_t ucm_event_installer_list;
43 
44 ucs_status_t ucm_set_mmap_hooks();
45 
46 void ucm_event_handler_add(ucm_event_handler_t *handler);
47 
48 void ucm_event_handler_remove(ucm_event_handler_t *handler);
49 
50 void ucm_event_dispatch(ucm_event_type_t event_type, ucm_event_t *event);
51 
52 void ucm_event_enter();
53 
54 void ucm_event_enter_exclusive();
55 
56 void ucm_event_leave();
57 
58 static UCS_F_ALWAYS_INLINE void
ucm_dispatch_vm_mmap(void * addr,size_t length)59 ucm_dispatch_vm_mmap(void *addr, size_t length)
60 {
61     ucm_event_t event;
62 
63     ucm_trace("vm_map addr=%p length=%zu", addr, length);
64 
65     event.vm_mapped.address = addr;
66     event.vm_mapped.size    = length;
67     ucm_event_dispatch(UCM_EVENT_VM_MAPPED, &event);
68 }
69 
70 static UCS_F_ALWAYS_INLINE void
ucm_dispatch_vm_munmap(void * addr,size_t length)71 ucm_dispatch_vm_munmap(void *addr, size_t length)
72 {
73     ucm_event_t event;
74 
75     ucm_trace("vm_unmap addr=%p length=%zu", addr, length);
76 
77     event.vm_unmapped.address = addr;
78     event.vm_unmapped.size    = length;
79     ucm_event_dispatch(UCM_EVENT_VM_UNMAPPED, &event);
80 }
81 
82 #endif
83