File: memory_linux_ummunotify.c

package info (click to toggle)
openmpi 1.6.5-9.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 91,628 kB
  • ctags: 44,305
  • sloc: ansic: 408,966; cpp: 44,454; sh: 27,828; makefile: 10,486; asm: 3,882; python: 1,239; lex: 805; perl: 549; csh: 253; fortran: 232; f90: 126; tcl: 12
file content (235 lines) | stat: -rw-r--r-- 8,279 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2009-2010 Cisco Systems, Inc.  All rights reserved.
 * $COPYRIGHT$
 * 
 * Additional copyrights may follow
 * 
 * $HEADER$
 */

#include "opal_config.h"

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef HAVE_STROPTS_H
#include <stropts.h>
#endif
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>

#include <linux/ummunotify.h>

#include "opal_stdint.h"
#include "opal/constants.h"
#include "opal/util/output.h"
#include "opal/util/show_help.h"
#include "opal/mca/memory/memory.h"
#include "opal/memoryhooks/memory.h"
#include "opal/memoryhooks/memory_internal.h"

#include "opal/mca/memory/linux/memory_linux.h"
#include "opal/mca/memory/linux/public.h"

#define DEV_UMMUNOTIFY "/dev/ummunotify"


/*
 * Local functions
 */
static int ummunotify_process(void);
static int ummunotify_register(void *start, size_t len, uint64_t cookie);
static int ummunotify_deregister(void *start, size_t len, uint64_t cookie);


/*
 * Local variables
 */
static bool initialized = false;


/*
 * Global variables (these need to be global variables rather than in
 * the component struct because they are accessed in the
 * opal_memory_changed() macro defined in public.h, and we don't want
 * to have to include the component structure definition in public.h).
 */
uint64_t opal_memory_linux_ummunotify_counter_last_value = 0;
volatile uint64_t *opal_memory_linux_ummunotify_counter = 
    &opal_memory_linux_ummunotify_counter_last_value;


int opal_memory_linux_ummunotify_open(void)
{
    uint64_t *p;

    /* Just to be safe... */
    opal_memory_linux_ummunotify_counter_last_value = 0;
    opal_memory_linux_ummunotify_counter =
        &opal_memory_linux_ummunotify_counter_last_value;

    /* Open the device.  Try to give a meaningful error message if
       we're unable to open it. */
    mca_memory_linux_component.ummunotify_fd = 
        open(DEV_UMMUNOTIFY, O_RDONLY | O_NONBLOCK);
    if (mca_memory_linux_component.ummunotify_fd < 0) {
        char hostname[HOST_NAME_MAX];
        gethostname(hostname, sizeof(hostname));

        if (EACCES == errno) {
            /* This will get a proper show_help when merged into the
               linux component */
            opal_show_help("help-opal-memory-linux.txt",
                           "ummunotify eaccess", true,
                           hostname, DEV_UMMUNOTIFY);
        } else if (ENOENT != errno) {
            /* Don't print an error if DEV_UMMUNOTIFY simply doesn't exist */
            opal_show_help("help-opal-memory-linux.txt",
                           "ummunotify open error", true,
                           hostname, DEV_UMMUNOTIFY, 
                           strerror(errno), errno);
        }
        return OPAL_ERR_NOT_SUPPORTED;
    }

    p = mmap(NULL, sizeof(*opal_memory_linux_ummunotify_counter), 
             PROT_READ, MAP_SHARED, 
             mca_memory_linux_component.ummunotify_fd, 0);
    if (MAP_FAILED == opal_memory_linux_ummunotify_counter) {
        close(mca_memory_linux_component.ummunotify_fd);
        mca_memory_linux_component.ummunotify_fd = -1;
        return OPAL_ERR_NOT_SUPPORTED;
    }
    opal_memory_linux_ummunotify_counter = p;

    /* If everything went well, tell OMPI that we have full support
       for the memory hooks and fill in the component function
       pointers */
    opal_mem_hooks_set_support(OPAL_MEMORY_FREE_SUPPORT | 
                               OPAL_MEMORY_CHUNK_SUPPORT |
                               OPAL_MEMORY_MUNMAP_SUPPORT);
    mca_memory_linux_component.super.memoryc_process = ummunotify_process;
    mca_memory_linux_component.super.memoryc_register = ummunotify_register;
    mca_memory_linux_component.super.memoryc_deregister = ummunotify_deregister;
    initialized = true;

    return OPAL_SUCCESS;
}


/*
 * Called during opal_finalize (usually during MPI_FINALIZE) to tear
 * down anything that can/should be torn down to disable this
 * component.  The application may continue for a while after
 * MPI_FINALIZE, so we should do as much as possible to disable
 * anything we enabled during ummunotify_open().
 */
int opal_memory_linux_ummunotify_close(void)
{
    if (initialized && mca_memory_linux_component.ummunotify_fd >= 0) {
        munmap((void*) opal_memory_linux_ummunotify_counter, 
               sizeof(*opal_memory_linux_ummunotify_counter));
        close(mca_memory_linux_component.ummunotify_fd);
        mca_memory_linux_component.ummunotify_fd = -1;
        opal_memory_linux_ummunotify_counter =
            &opal_memory_linux_ummunotify_counter_last_value;
        initialized = false;
    }

    return OPAL_SUCCESS;
}

/*
 * Called when opal_memory_changed() returns 1
 */
static int ummunotify_process(void)
{
    int n;
    unsigned int i;
    struct ummunotify_event events[128];

    /* Loop reading from the ummunot fd until there's nothing left to
       read.  If we get a LAST event, re-record the counter. */
    while (initialized) {
        n = read(mca_memory_linux_component.ummunotify_fd, 
                 &events, sizeof(events));
        if (n <= 0) {
            return (EAGAIN == errno) ? OPAL_SUCCESS : OPAL_ERR_IN_ERRNO;
        }

        for (i = 0; i < n / sizeof(events[0]); ++i) {
            switch (events[i].type) {
            case UMMUNOTIFY_EVENT_TYPE_INVAL:
                /* 0 => this callback did not come from malloc */
                OPAL_OUTPUT((-1, "ummunot: invalidate start %p, end %p", 
                             (void*) events[i].hint_start,
                             (void*) events[i].hint_end));
                opal_mem_hooks_release_hook((void *) (uintptr_t) events[i].hint_start,
                                            events[i].hint_end - events[i].hint_start,
                                            0);
                break;

            case UMMUNOTIFY_EVENT_TYPE_LAST:
                opal_memory_linux_ummunotify_counter_last_value = 
                    events[i].user_cookie_counter;
                /* Are there more events to read? */
                if (opal_memory_linux_ummunotify_counter_last_value ==
                    *opal_memory_linux_ummunotify_counter) {
                    OPAL_OUTPUT((-1, "ummunot: LAST; done"));
                    return OPAL_SUCCESS;
                }
                OPAL_OUTPUT((-1, "ummunot: LAST; but looping around"));
                break;
            }
        }
    }

    /* Will only get here if this component has not been
       initialized */
    return OPAL_SUCCESS;
}

static int ummunotify_register(void *start, size_t len, uint64_t cookie)
{
    struct ummunotify_register_ioctl r;
    r.reserved = 0;
    r.start = (unsigned long) start;
    r.end = (unsigned long) start + len;
    r.user_cookie = cookie;

    OPAL_OUTPUT((-1, "ummunot: register %p - %p", 
                 start, ((char*) start) + len));
    if (initialized && ioctl(mca_memory_linux_component.ummunotify_fd,
                             UMMUNOTIFY_REGISTER_REGION, &r)) {
        OPAL_OUTPUT((-1, "Error in ioctl register!"));
        return OPAL_ERR_IN_ERRNO;
    }
    
    return OPAL_SUCCESS;
}

static int ummunotify_deregister(void *start, size_t len, uint64_t cookie)
{
    OPAL_OUTPUT((-1, "ummunot: deregister %p - %p", 
                 start, ((char*) start) + len));
    if (initialized && ioctl(mca_memory_linux_component.ummunotify_fd, 
                             UMMUNOTIFY_UNREGISTER_REGION, &cookie)) {
        OPAL_OUTPUT((-1, "Error in ioctl unregister!"));
        return OPAL_ERR_IN_ERRNO;
    }

    return OPAL_SUCCESS;
}