1 /*****************************************************************************\
2  *  site_factor.c - site priority factor driver
3  *****************************************************************************
4  *  Copyright (C) 2019 SchedMD LLC
5  *  Written by Tim Wickberg <tim@schedmd.com>
6  *
7  *  This file is part of Slurm, a resource management program.
8  *  For details, see <https://slurm.schedmd.com/>.
9  *  Please also read the included file: DISCLAIMER.
10  *
11  *  Slurm is free software; you can redistribute it and/or modify it under
12  *  the terms of the GNU General Public License as published by the Free
13  *  Software Foundation; either version 2 of the License, or (at your option)
14  *  any later version.
15  *
16  *  In addition, as a special exception, the copyright holders give permission
17  *  to link the code of portions of this program with the OpenSSL library under
18  *  certain conditions as described in each individual source file, and
19  *  distribute linked combinations including the two. You must obey the GNU
20  *  General Public License in all respects for all of the code used other than
21  *  OpenSSL. If you modify file(s) with this exception, you may extend this
22  *  exception to your version of the file(s), but you are not obligated to do
23  *  so. If you do not wish to do so, delete this exception statement from your
24  *  version.  If you delete this exception statement from all source files in
25  *  the program, then also delete it here.
26  *
27  *  Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
28  *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
29  *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
30  *  details.
31  *
32  *  You should have received a copy of the GNU General Public License along
33  *  with Slurm; if not, write to the Free Software Foundation, Inc.,
34  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
35 \*****************************************************************************/
36 
37 #include "src/common/plugin.h"
38 #include "src/common/site_factor.h"
39 #include "src/common/timers.h"
40 
41 #define SITE_FACTOR_TIMER 50000			/* 50 milliseconds */
42 #define SITE_FACTOR_TIMER_RECONFIG 500000	/* 500 milliseconds */
43 
44 /* Symbols provided by the plugin */
45 typedef struct slurm_ops {
46 	void	(*reconfig)	(void);
47 	void	(*set)		(job_record_t *job_ptr);
48 	void	(*update)	(void);
49 } slurm_ops_t;
50 
51 /*
52  * These strings must be kept in the same order as the fields
53  * declared for slurm_ops_t.
54  */
55 static const char *syms[] = {
56 	"site_factor_p_reconfig",
57 	"site_factor_p_set",
58 	"site_factor_p_update",
59 };
60 
61 /* Local variables */
62 static slurm_ops_t ops;
63 static plugin_context_t *g_context = NULL;
64 static pthread_mutex_t g_context_lock =	PTHREAD_MUTEX_INITIALIZER;
65 static bool init_run = false;
66 
67 /*
68  * Initialize the site_factor plugin.
69  *
70  * Returns a Slurm errno.
71  */
site_factor_plugin_init(void)72 extern int site_factor_plugin_init(void)
73 {
74 	int retval = SLURM_SUCCESS;
75 	char *plugin_type = "site_factor";
76 	char *type = NULL;
77 
78 	if (init_run && g_context)
79 		return retval;
80 
81 	slurm_mutex_lock(&g_context_lock);
82 
83 	if (g_context)
84 		goto done;
85 
86 	type = slurm_get_priority_site_factor_plugin();
87 
88 	g_context = plugin_context_create(plugin_type, type, (void **)&ops,
89 					  syms, sizeof(syms));
90 
91 	if (!g_context) {
92 		error("cannot create %s context for %s", plugin_type, type);
93 		retval = SLURM_ERROR;
94 		goto done;
95 	}
96 
97 	init_run = true;
98 	debug2("%s: plugin %s loaded", __func__, type);
99 
100 done:
101 	slurm_mutex_unlock(&g_context_lock);
102 	xfree(type);
103 
104 	return retval;
105 }
106 
site_factor_plugin_fini(void)107 extern int site_factor_plugin_fini(void)
108 {
109 	int rc;
110 
111 	if (!g_context)
112 		return SLURM_SUCCESS;
113 
114 	slurm_mutex_lock(&g_context_lock);
115 	init_run = false;
116 	rc = plugin_context_destroy(g_context);
117 	g_context = NULL;
118 	slurm_mutex_unlock(&g_context_lock);
119 
120 	return rc;
121 }
122 
site_factor_g_reconfig(void)123 extern void site_factor_g_reconfig(void)
124 {
125 	DEF_TIMERS;
126 
127 	if (site_factor_plugin_init() < 0)
128 		return;
129 
130 	START_TIMER;
131 	(*(ops.reconfig))();
132 	END_TIMER3(__func__, SITE_FACTOR_TIMER_RECONFIG);
133 }
134 
site_factor_g_set(job_record_t * job_ptr)135 extern void site_factor_g_set(job_record_t *job_ptr)
136 {
137 	DEF_TIMERS;
138 	if (site_factor_plugin_init() < 0)
139 		return;
140 
141 	START_TIMER;
142 	(*(ops.set))(job_ptr);
143 	END_TIMER3(__func__, SITE_FACTOR_TIMER);
144 }
145 
site_factor_g_update(void)146 extern void site_factor_g_update(void)
147 {
148 	DEF_TIMERS;
149 
150 	if (site_factor_plugin_init() < 0)
151 		return;
152 
153 	START_TIMER;
154 	(*(ops.update))();
155 	END_TIMER3(__func__, SITE_FACTOR_TIMER);
156 }
157