Debian Codegraph @

hmmer/src/hmmpgmd.c

raw symbols SCIP
1 /* HMMER search daemon
2 */
3 #include <p7_config.h>
4
5 #ifdef HMMER_THREADS
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <signal.h>
11
12 #ifndef HMMER_THREADS
13 #error "Program requires pthreads be enabled."
14 #endif /*HMMER_THREADS*/
15
16 #include "easel.h"
17 #include "esl_getopts.h"
18
19 #include "hmmer.h"
20 #include "hmmpgmd.h"
21
22 #define CONF_FILE "/etc/hmmpgmd.conf"
23
24 static ESL_OPTIONS cmdlineOpts[] = {
25 /* name type default env range toggles reqs incomp help docgroup */
26 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 },
27 { "--master", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--worker", "run program as the master server", 12 },
28 { "--worker", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, "--master", "run program as a worker with server at <s>", 12 },
29 { "--cport", eslARG_INT, "51371", NULL, "49151<n<65536",NULL, NULL, "--worker", "port to use for client/server communication", 12 },
30 { "--wport", eslARG_INT, "51372", NULL, "49151<n<65536",NULL, NULL, NULL, "port to use for server/worker communication", 12 },
31 { "--ccncts", eslARG_INT, "16", NULL, "n>0", NULL, NULL, "--worker", "maximum number of client side connections to accept", 12 },
32 { "--wcncts", eslARG_INT, "32", NULL, "n>0", NULL, NULL, "--worker", "maximum number of worker side connections to accept", 12 },
33 { "--pid", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "file to write process id to", 12 },
34 { "--seqdb", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, "--worker", "protein database to cache for searches", 12 },
35 { "--hmmdb", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, "--worker", "hmm database to cache for searches", 12 },
36 { "--cpu", eslARG_INT, p7_NCPU,"HMMER_NCPU","n>0", NULL, NULL, "--master", "number of parallel CPU workers to use for multithreads", 12 },
37 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
38
39 };
40
41 static char usage[] = "[options]";
42 static char banner[] = "search a query against a database";
43
44
45 typedef void sig_func(int);
46
47 /* MSF called this <signal()>. That conflicts with system's <signal()>.
48 * Unclear what he was doing. This appears to duplicated functionality
49 * of the system's <signal()>. Revisit later. For now, silence compiler
50 * warning by renaming the function.
51 */
52 sig_func *
53 our_signal(int signo, sig_func *fn)
54 {
55 struct sigaction act;
56 struct sigaction oact;
57
58 act.sa_handler = fn;
59 sigemptyset(&act.sa_mask);
60 act.sa_flags = 0;
61 if (signo == SIGALRM) {
62 #ifdef SA_INTERRUPT
63 act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
64 #endif
65 } else {
66 #ifdef SA_RESTART
67 act.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */
68 #endif
69 }
70 if (sigaction(signo, &act, &oact) < 0) {
71 return SIG_ERR;
72 }
73
74 return oact.sa_handler;
75 }
76
77 /* write_pid()
78 * Log the process id to a file.
79 */
80 static void
81 write_pid(ESL_GETOPTS *go)
82 {
83 char *pid_file = esl_opt_GetString(go, "--pid");
84 FILE *fp = fopen(pid_file, "w");
85
86 if (!fp) p7_Fail("Unable to open PID file %s for writing.", pid_file);
87 fprintf(fp,"%ld\n", (long)getpid());
88 fclose(fp);
89 }
90
91 static int
92 process_commandline(int argc, char **argv, ESL_GETOPTS **ret_go)
93 {
94 ESL_GETOPTS *go = esl_getopts_Create(cmdlineOpts);
95 int status;
96
97 /* if there are no command line arguments, let's try to read /etc/hmmpgmd.conf
98 * for any configuration data.
99 */
100 if (argc == 1)
101 {
102 FILE *fp = NULL;
103
104 if ((fp = fopen(CONF_FILE, "r")) == NULL)
105 { if (puts("Options --master or --worker must be specified.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
106
107 if ((status = esl_opt_ProcessConfigfile(go, CONF_FILE, fp) ) != eslOK)
108 { if (printf("Failed to parse configuration file %s: %s\n", CONF_FILE, go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
109 fclose(fp);
110 }
111 else
112 {
113 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK)
114 { if (printf("Failed to parse command line: %s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
115 }
116
117 if (esl_opt_VerifyConfig(go) != eslOK) { if (printf("Failed to parse command line: %s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
118
119 /* help format: */
120 if (esl_opt_GetBoolean(go, "-h") == TRUE)
121 {
122 p7_banner(stdout, argv[0], banner);
123 esl_usage(stdout, argv[0], usage);
124
125 if (puts("\nBasic options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
126 esl_opt_DisplayHelp(stdout, go, 1, 2, 80); /* 1= group; 2 = indentation; 80=textwidth*/
127
128 if (puts("\nOther expert options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
129 esl_opt_DisplayHelp(stdout, go, 12, 2, 80);
130 exit(0);
131 }
132
133
134 if (esl_opt_ArgNumber(go) != 0) { if (puts("Incorrect number of command line arguments.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
135
136 if (esl_opt_IsUsed(go, "--master") && !(esl_opt_IsUsed(go, "--seqdb") || esl_opt_IsUsed(go, "--hmmdb")))
137 { if (puts("At least one --seqdb or --hmmdb must be specified.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
138
139 *ret_go = go;
140 return eslOK;
141
142 FAILURE: /* all errors handled here are user errors, so be polite. */
143 esl_usage(stdout, argv[0], usage);
144 puts("\nwhere most common options are:");
145 esl_opt_DisplayHelp(stdout, go, 1, 2, 80); /* 1= group; 2 = indentation; 80=textwidth*/
146 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
147 esl_getopts_Destroy(go);
148 exit(0);
149
150 ERROR:
151 if (go) esl_getopts_Destroy(go);
152 exit(status);
153 }
154
155 int
156 main(int argc, char **argv)
157 {
158 ESL_GETOPTS *go = NULL; /* command line processing */
159
160 process_commandline(argc, argv, &go);
161
162 /* if we write to a broken socket, ignore the signal and handle the error. */
163 our_signal(SIGPIPE, SIG_IGN);
164
165 /* check if we need to write out our pid */
166 if (esl_opt_IsOn(go, "--pid")) write_pid(go);
167
168 if (esl_opt_IsUsed(go, "--master")) master_process(go);
169 else if (esl_opt_IsUsed(go, "--worker")) worker_process(go);
170 else
171 { puts("Options --master or --worker must be specified."); }
172
173 esl_getopts_Destroy(go);
174
175 return eslOK;
176 }
177
178
179 /*****************************************************************
180 * Notes on testing and debugging
181 *****************************************************************/
182
183
184 /* How to run a test:
185 * Need a test sequence database: mine is ~eddys/data/hmmpgmd/hmmpgmd.fa
186 * Need a test profile database: mine is ~eddys/data/hmmpgmd/hmmpgmd.hmm
187 *
188 * src/hmmpgmd --master --seqdb ~/data/hmmpgmd/hmmpgmd.fa --hmmdb ~/data/hmmpgmd/hmmpgmd.hmm &
189 * src/hmmpgmd --worker 127.0.0.1 --cpu 1 &
190 * src/hmmc2 -S
191 * @--seqdb 1
192 * >foo
193 * ACDEFGH...
194 * //
195 * !shutdown
196 * //
197 *
198 * Or, for hmmscan against the hmm db, replace @--seqdb 1 with @--hmmdb 1.
199 *
200 * For debugging, start two of the three processes on cmdline, and the
201 * one to be debugged under gdb.
202 * in worker: break process_SearchCmd
203 *
204 *
205 * Valgrind debugging
206 * generate a small test seq database:
207 * esl-shuffle -G --amino -N 10 -L 400 > foo.fa
208 * fasta2daemon.pl foo.fa > foo.d
209 *
210 * in three separate terminal windows
211 * valgrind --dsymutil=yes --leak-check=yes --show-reachable=yes src/hmmpgmd --master --seqdb foo.d
212 * valgrind --leak-check=yes --show-reachable=yes src/hmmpgmd --worker 127.0.0.1 --cpu 1
213 * valgrind --dsymutil=yes --leak-check=yes --show-reachable=yes src/hmmc2 -S
214 *
215 * copy/paste tutorial/M1.hmm as an example hmmsearch
216 * or any HMM. On Mac OS/X, to "copy" a file my.hmm to the clipboard:
217 * cat my.hmm | pbcopy
218 */
219
220 #endif /*HMMER_THREADS*/
221