]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/index.c
more multi-processor improvements
[nominatim.git] / nominatim / index.c
1 /*
2 */
3
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <pthread.h>
10 #include <time.h>
11 #include <stdint.h>
12
13 #include <libpq-fe.h>
14
15 #include "nominatim.h"
16 #include "index.h"
17 #include "export.h"
18 #include "postgresql.h"
19
20 extern int verbose;
21
22 void nominatim_index(int rank_min, int rank_max, int num_threads, const char *conninfo, const char *structuredoutputfile)
23 {
24         struct index_thread_data * thread_data;
25         pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
26         int tuples, count, sleepcount;
27
28         time_t rankStartTime;
29         int rankTotalTuples;
30         int rankCountTuples;
31         float rankPerSecond;
32
33         PGconn *conn;
34         PGresult * res;
35         PGresult * resSectors;
36         PGresult * resPlaces;
37
38         int rank;
39         int i;
40         int iSector;
41
42     const char *paramValues[2];
43     int         paramLengths[2];
44     int         paramFormats[2];
45     uint32_t    paramRank;
46     uint32_t    paramSector;
47     uint32_t    sector;
48
49     xmlTextWriterPtr writer;
50         pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER;
51
52     Oid pg_prepare_params[2];
53
54     conn = PQconnectdb(conninfo);
55     if (PQstatus(conn) != CONNECTION_OK) {
56         fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
57         exit(EXIT_FAILURE);
58     }
59
60     pg_prepare_params[0] = PG_OID_INT4;
61     res = PQprepare(conn, "index_sectors",
62         "select geometry_sector,count(*) from placex where rank_search = $1 and indexed_status > 0 group by geometry_sector order by geometry_sector",
63         1, pg_prepare_params);
64     if (PQresultStatus(res) != PGRES_COMMAND_OK)
65     {
66         fprintf(stderr, "Failed preparing index_sectors: %s\n", PQerrorMessage(conn));
67         exit(EXIT_FAILURE);
68     }
69     PQclear(res);
70
71     pg_prepare_params[0] = PG_OID_INT4;
72     res = PQprepare(conn, "index_nosectors",
73         "select 0::integer,count(*) from placex where rank_search = $1 and indexed_status > 0",
74         1, pg_prepare_params);
75     if (PQresultStatus(res) != PGRES_COMMAND_OK)
76     {
77         fprintf(stderr, "Failed preparing index_sectors: %s\n", PQerrorMessage(conn));
78         exit(EXIT_FAILURE);
79     }
80     PQclear(res);
81
82     pg_prepare_params[0] = PG_OID_INT4;
83     pg_prepare_params[1] = PG_OID_INT4;
84     res = PQprepare(conn, "index_sector_places",
85         "select place_id from placex where rank_search = $1 and geometry_sector = $2 and indexed_status > 0",
86         2, pg_prepare_params);
87     if (PQresultStatus(res) != PGRES_COMMAND_OK)
88     {
89         fprintf(stderr, "Failed preparing index_sector_places: %s\n", PQerrorMessage(conn));
90         exit(EXIT_FAILURE);
91     }
92     PQclear(res);
93
94     pg_prepare_params[0] = PG_OID_INT4;
95     res = PQprepare(conn, "index_nosector_places",
96         "select place_id from placex where rank_search = $1 and indexed_status > 0 order by geometry_sector",
97         1, pg_prepare_params);
98     if (PQresultStatus(res) != PGRES_COMMAND_OK)
99     {
100         fprintf(stderr, "Failed preparing index_nosector_places: %s\n", PQerrorMessage(conn));
101         exit(EXIT_FAILURE);
102     }
103     PQclear(res);
104
105     // Build the data for each thread
106     thread_data = (struct index_thread_data *)malloc(sizeof(struct index_thread_data)*num_threads);
107         for (i = 0; i < num_threads; i++)
108         {
109                 thread_data[i].conn = PQconnectdb(conninfo);
110             if (PQstatus(thread_data[i].conn) != CONNECTION_OK) {
111                 fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(thread_data[i].conn));
112                 exit(EXIT_FAILURE);
113             }
114
115             pg_prepare_params[0] = PG_OID_INT4;
116             res = PQprepare(thread_data[i].conn, "index_placex",
117                 "update placex set indexed_status = 0 where place_id = $1",
118                 1, pg_prepare_params);
119             if (PQresultStatus(res) != PGRES_COMMAND_OK)
120             {
121                 fprintf(stderr, "Failed preparing index_placex: %s\n", PQerrorMessage(conn));
122                 exit(EXIT_FAILURE);
123             }
124             PQclear(res);
125
126             nominatim_exportCreatePreparedQueries(thread_data[i].conn);
127         }
128
129         // Create the output file
130         writer = NULL;
131         if (structuredoutputfile)
132         {
133                 writer = nominatim_exportXMLStart(structuredoutputfile);
134         }
135
136     fprintf(stderr, "Starting indexing rank (%i > %i ) using %i treads\n", rank_min, rank_max, num_threads);
137
138     for (rank = rank_min; rank <= rank_max; rank++)
139     {
140         printf("Starting rank %d\n", rank);
141         rankStartTime = time(0);
142         rankCountTuples = 0;
143         rankPerSecond = 0;
144
145         paramRank = PGint32(rank);
146         paramValues[0] = (char *)&paramRank;
147         paramLengths[0] = sizeof(paramRank);
148         paramFormats[0] = 1;
149                 if (rank < 16)
150                 resSectors = PQexecPrepared(conn, "index_nosectors", 1, paramValues, paramLengths, paramFormats, 1);
151                 else
152                 resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
153         if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
154         {
155             fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
156             PQclear(resSectors);
157             exit(EXIT_FAILURE);
158         }
159                 if (PQftype(resSectors, 0) != PG_OID_INT4)
160                 {
161             fprintf(stderr, "Sector value has unexpected type\n");
162             PQclear(resSectors);
163             exit(EXIT_FAILURE);
164                 }
165                 if (PQftype(resSectors, 1) != PG_OID_INT8)
166                 {
167             fprintf(stderr, "Sector value has unexpected type\n");
168             PQclear(resSectors);
169             exit(EXIT_FAILURE);
170                 }
171
172                 rankTotalTuples = 0;
173         for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
174         {
175                 rankTotalTuples += PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1)));
176         }
177
178         for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
179         {
180                         sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
181                 //printf("\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
182
183                         // Get all the place_id's for this sector
184                     paramRank = PGint32(rank);
185                 paramValues[0] = (char *)&paramRank;
186                     paramLengths[0] = sizeof(paramRank);
187                     paramFormats[0] = 1;
188                 paramSector = PGint32(sector);
189                 paramValues[1] = (char *)&paramSector;
190                         paramLengths[1] = sizeof(paramSector);
191                     paramFormats[1] = 1;
192                         if (rank < 16)
193                         resPlaces = PQexecPrepared(conn, "index_nosector_places", 1, paramValues, paramLengths, paramFormats, 1);
194                         else
195                         resPlaces = PQexecPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
196                 if (PQresultStatus(resPlaces) != PGRES_TUPLES_OK)
197                 {
198                     fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
199                     PQclear(resPlaces);
200                     exit(EXIT_FAILURE);
201                 }
202                         if (PQftype(resPlaces, 0) != PG_OID_INT4)
203                         {
204                     fprintf(stderr, "Place_id value has unexpected type\n");
205                     PQclear(resPlaces);
206                     exit(EXIT_FAILURE);
207                         }
208
209                         count = 0;
210                         rankPerSecond = 0;
211                         tuples = PQntuples(resPlaces);
212
213                         if (tuples > 0)
214                 {
215                                 // Spawn threads
216                                 for (i = 0; i < num_threads; i++)
217                                 {
218                                         thread_data[i].res = resPlaces;
219                                         thread_data[i].tuples = tuples;
220                                         thread_data[i].count = &count;
221                                         thread_data[i].count_mutex = &count_mutex;
222                                         thread_data[i].writer = writer;
223                                         thread_data[i].writer_mutex = &writer_mutex;
224                                         pthread_create(&thread_data[i].thread, NULL, &nominatim_indexThread, (void *)&thread_data[i]);
225                                 }
226
227                                 // Monitor threads to give user feedback
228                                 sleepcount = 0;
229                                 while(count < tuples)
230                                 {
231                                         usleep(1000);
232
233                                         // Aim for one update per second
234                                         if (sleepcount++ > 500)
235                                         {
236                                                 rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
237                                                 printf("  Done %i in %i @ %f per second - Rank %i ETA (seconds): %f\n", (rankCountTuples + count), (int)(difftime(time(0), rankStartTime)), rankPerSecond, rank, ((float)(rankTotalTuples - (rankCountTuples + count)))/rankPerSecond);
238                                                 sleepcount = 0;
239                                         }
240                                 }
241
242                                 // Wait for everything to finish
243                                 for (i = 0; i < num_threads; i++)
244                                 {
245                                         pthread_join(thread_data[i].thread, NULL);
246                                 }
247
248                                 rankCountTuples += tuples;
249                 }
250
251                         // Finished sector
252                         rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
253                         printf("  Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
254
255             PQclear(resPlaces);
256
257         }
258         // Finished rank
259                 printf("\r  Done %i in %i @ %f per second - FINISHED                      \n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
260
261         PQclear(resSectors);
262     }
263
264     if (writer)
265     {
266         nominatim_exportXMLEnd(writer);
267     }
268 }
269
270 void *nominatim_indexThread(void * thread_data_in)
271 {
272         struct index_thread_data * thread_data = (struct index_thread_data * )thread_data_in;
273
274         PGresult * res;
275
276     const char *paramValues[1];
277     int         paramLengths[1];
278     int         paramFormats[1];
279     uint32_t    paramPlaceID;
280     uint32_t place_id;
281
282         while(1)
283         {
284                 pthread_mutex_lock( thread_data->count_mutex );
285                 if (*(thread_data->count) >= thread_data->tuples)
286                 {
287                         pthread_mutex_unlock( thread_data->count_mutex );
288                         break;
289                 }
290
291                 place_id = PGint32(*((uint32_t *)PQgetvalue(thread_data->res, *thread_data->count, 0)));
292                 (*thread_data->count)++;
293
294                 pthread_mutex_unlock( thread_data->count_mutex );
295
296 //              printf("  Processing place_id %d\n", place_id);
297                 paramPlaceID = PGint32(place_id);
298         paramValues[0] = (char *)&paramPlaceID;
299         paramLengths[0] = sizeof(paramPlaceID);
300         paramFormats[0] = 1;
301         res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
302         if (PQresultStatus(res) != PGRES_COMMAND_OK)
303         {
304             fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
305             PQclear(res);
306             exit(EXIT_FAILURE);
307         }
308         PQclear(res);
309
310         if (thread_data->writer)
311         {
312                 nominatim_exportPlace(place_id, thread_data->conn, thread_data->writer, thread_data->writer_mutex);
313         }
314         }
315
316         return NULL;
317 }