1 // Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License, version 2.0, as
5 // published by the Free Software Foundation.
6 //
7 // This program is also distributed with certain software (including
8 // but not limited to OpenSSL) that is licensed under separate terms,
9 // as designated in a particular file or component or in included license
10 // documentation. The authors of MySQL hereby grant you an
11 // additional permission to link the program and your derivative works
12 // with the separately licensed software that they have included with
13 // MySQL.
14 //
15 // Without limiting anything contained in the foregoing, this file,
16 // which is part of MySQL Server, is also subject to the
17 // Universal FOSS Exception, version 1.0, a copy of which can be found at
18 // http://oss.oracle.com/licenses/universal-foss-exception.
19 //
20 // This program is distributed in the hope that it will be useful, but
21 // WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 // See the GNU General Public License, version 2.0, for more details.
24 //
25 // You should have received a copy of the GNU General Public License
26 // along with this program; if not, write to the Free Software Foundation, Inc.,
27 // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 
29 /**
30   @file include/mysql/psi/mysql_file.h
31   Instrumentation helpers for mysys file io.
32   This header file provides the necessary declarations
33   to use the mysys file API with the performance schema instrumentation.
34   In some compilers (SunStudio), 'static inline' functions, when declared
35   but not used, are not optimized away (because they are unused) by default,
36   so that including a static inline function from a header file does
37   create unwanted dependencies, causing unresolved symbols at link time.
38   Other compilers, like gcc, optimize these dependencies by default.
39 
40   Since the instrumented APIs declared here are wrapper on top
41   of mysys file io APIs, including mysql/psi/mysql_file.h assumes that
42   the dependency on my_sys already exists.
43 */
44 
45 #ifndef MYSQL_FILE_H
46 #define MYSQL_FILE_H
47 
48 /* For strlen() */
49 #include <string.h>
50 
51 #include "my_dbug.h"
52 /* For MY_STAT */
53 #include "my_dir.h"
54 /* For my_chsize */
55 #include "my_sys.h"
56 #include "mysql/psi/psi_file.h"
57 #include "mysql/service_mysql_alloc.h"
58 #include "pfs_file_provider.h"
59 
60 #ifndef PSI_FILE_CALL
61 #define PSI_FILE_CALL(M) psi_file_service->M
62 #endif
63 
64 /**
65   @defgroup psi_api_file File Instrumentation (API)
66   @ingroup psi_api
67   @{
68 */
69 
70 /**
71   @def mysql_file_register(P1, P2, P3)
72   File registration.
73 */
74 #define mysql_file_register(P1, P2, P3) inline_mysql_file_register(P1, P2, P3)
75 
76 /**
77   @def mysql_file_fgets(P1, P2, F)
78   Instrumented fgets.
79   @c mysql_file_fgets is a replacement for @c fgets.
80 */
81 #ifdef HAVE_PSI_FILE_INTERFACE
82 #define mysql_file_fgets(P1, P2, F) \
83   inline_mysql_file_fgets(__FILE__, __LINE__, P1, P2, F)
84 #else
85 #define mysql_file_fgets(P1, P2, F) inline_mysql_file_fgets(P1, P2, F)
86 #endif
87 
88 /**
89   @def mysql_file_fgetc(F)
90   Instrumented fgetc.
91   @c mysql_file_fgetc is a replacement for @c fgetc.
92 */
93 #ifdef HAVE_PSI_FILE_INTERFACE
94 #define mysql_file_fgetc(F) inline_mysql_file_fgetc(__FILE__, __LINE__, F)
95 #else
96 #define mysql_file_fgetc(F) inline_mysql_file_fgetc(F)
97 #endif
98 
99 /**
100   @def mysql_file_fputs(P1, F)
101   Instrumented fputs.
102   @c mysql_file_fputs is a replacement for @c fputs.
103 */
104 #ifdef HAVE_PSI_FILE_INTERFACE
105 #define mysql_file_fputs(P1, F) \
106   inline_mysql_file_fputs(__FILE__, __LINE__, P1, F)
107 #else
108 #define mysql_file_fputs(P1, F) inline_mysql_file_fputs(P1, F)
109 #endif
110 
111 /**
112   @def mysql_file_fputc(P1, F)
113   Instrumented fputc.
114   @c mysql_file_fputc is a replacement for @c fputc.
115 */
116 #ifdef HAVE_PSI_FILE_INTERFACE
117 #define mysql_file_fputc(P1, F) \
118   inline_mysql_file_fputc(__FILE__, __LINE__, P1, F)
119 #else
120 #define mysql_file_fputc(P1, F) inline_mysql_file_fputc(P1, F)
121 #endif
122 
123 /**
124   @def mysql_file_fprintf
125   Instrumented fprintf.
126   @c mysql_file_fprintf is a replacement for @c fprintf.
127 */
128 #define mysql_file_fprintf inline_mysql_file_fprintf
129 
130 /**
131   @def mysql_file_vfprintf(F, P1, P2)
132   Instrumented vfprintf.
133   @c mysql_file_vfprintf is a replacement for @c vfprintf.
134 */
135 #ifdef HAVE_PSI_FILE_INTERFACE
136 #define mysql_file_vfprintf(F, P1, P2) \
137   inline_mysql_file_vfprintf(__FILE__, __LINE__, F, P1, P2)
138 #else
139 #define mysql_file_vfprintf(F, P1, P2) inline_mysql_file_vfprintf(F, P1, P2)
140 #endif
141 
142 /**
143   @def mysql_file_fflush(F, P1, P2)
144   Instrumented fflush.
145   @c mysql_file_fflush is a replacement for @c fflush.
146 */
147 #ifdef HAVE_PSI_FILE_INTERFACE
148 #define mysql_file_fflush(F) inline_mysql_file_fflush(__FILE__, __LINE__, F)
149 #else
150 #define mysql_file_fflush(F) inline_mysql_file_fflush(F)
151 #endif
152 
153 /**
154   @def mysql_file_feof(F)
155   Instrumented feof.
156   @c mysql_file_feof is a replacement for @c feof.
157 */
158 #define mysql_file_feof(F) inline_mysql_file_feof(F)
159 
160 /**
161   @def mysql_file_fstat(FN, S)
162   Instrumented fstat.
163   @c mysql_file_fstat is a replacement for @c my_fstat.
164 */
165 #ifdef HAVE_PSI_FILE_INTERFACE
166 #define mysql_file_fstat(FN, S) \
167   inline_mysql_file_fstat(__FILE__, __LINE__, FN, S)
168 #else
169 #define mysql_file_fstat(FN, S) inline_mysql_file_fstat(FN, S)
170 #endif
171 
172 /**
173   @def mysql_file_stat(K, FN, S, FL)
174   Instrumented stat.
175   @c mysql_file_stat is a replacement for @c my_stat.
176 */
177 #ifdef HAVE_PSI_FILE_INTERFACE
178 #define mysql_file_stat(K, FN, S, FL) \
179   inline_mysql_file_stat(K, __FILE__, __LINE__, FN, S, FL)
180 #else
181 #define mysql_file_stat(K, FN, S, FL) inline_mysql_file_stat(FN, S, FL)
182 #endif
183 
184 /**
185   @def mysql_file_chsize(F, P1, P2, P3)
186   Instrumented chsize.
187   @c mysql_file_chsize is a replacement for @c my_chsize.
188 */
189 #ifdef HAVE_PSI_FILE_INTERFACE
190 #define mysql_file_chsize(F, P1, P2, P3) \
191   inline_mysql_file_chsize(__FILE__, __LINE__, F, P1, P2, P3)
192 #else
193 #define mysql_file_chsize(F, P1, P2, P3) inline_mysql_file_chsize(F, P1, P2, P3)
194 #endif
195 
196 /**
197   @def mysql_file_fopen(K, N, F1, F2)
198   Instrumented fopen.
199   @c mysql_file_fopen is a replacement for @c my_fopen.
200 */
201 #ifdef HAVE_PSI_FILE_INTERFACE
202 #define mysql_file_fopen(K, N, F1, F2) \
203   inline_mysql_file_fopen(K, __FILE__, __LINE__, N, F1, F2)
204 #else
205 #define mysql_file_fopen(K, N, F1, F2) inline_mysql_file_fopen(N, F1, F2)
206 #endif
207 
208 /**
209   @def mysql_file_fclose(FD, FL)
210   Instrumented fclose.
211   @c mysql_file_fclose is a replacement for @c my_fclose.
212   Without the instrumentation, this call will have the same behavior as the
213   undocumented and possibly platform specific my_fclose(NULL, ...) behavior.
214   With the instrumentation, mysql_fclose(NULL, ...) will safely return 0,
215   which is an extension compared to my_fclose and is therefore compliant.
216   mysql_fclose is on purpose *not* implementing
217   @code DBUG_ASSERT(file != NULL) @endcode,
218   since doing so could introduce regressions.
219 */
220 #ifdef HAVE_PSI_FILE_INTERFACE
221 #define mysql_file_fclose(FD, FL) \
222   inline_mysql_file_fclose(__FILE__, __LINE__, FD, FL)
223 #else
224 #define mysql_file_fclose(FD, FL) inline_mysql_file_fclose(FD, FL)
225 #endif
226 
227 /**
228   @def mysql_file_fread(FD, P1, P2, P3)
229   Instrumented fread.
230   @c mysql_file_fread is a replacement for @c my_fread.
231 */
232 #ifdef HAVE_PSI_FILE_INTERFACE
233 #define mysql_file_fread(FD, P1, P2, P3) \
234   inline_mysql_file_fread(__FILE__, __LINE__, FD, P1, P2, P3)
235 #else
236 #define mysql_file_fread(FD, P1, P2, P3) inline_mysql_file_fread(FD, P1, P2, P3)
237 #endif
238 
239 /**
240   @def mysql_file_fwrite(FD, P1, P2, P3)
241   Instrumented fwrite.
242   @c mysql_file_fwrite is a replacement for @c my_fwrite.
243 */
244 #ifdef HAVE_PSI_FILE_INTERFACE
245 #define mysql_file_fwrite(FD, P1, P2, P3) \
246   inline_mysql_file_fwrite(__FILE__, __LINE__, FD, P1, P2, P3)
247 #else
248 #define mysql_file_fwrite(FD, P1, P2, P3) \
249   inline_mysql_file_fwrite(FD, P1, P2, P3)
250 #endif
251 
252 /**
253   @def mysql_file_fseek(FD, P, W)
254   Instrumented fseek.
255   @c mysql_file_fseek is a replacement for @c my_fseek.
256 */
257 #ifdef HAVE_PSI_FILE_INTERFACE
258 #define mysql_file_fseek(FD, P, W) \
259   inline_mysql_file_fseek(__FILE__, __LINE__, FD, P, W)
260 #else
261 #define mysql_file_fseek(FD, P, W) inline_mysql_file_fseek(FD, P, W)
262 #endif
263 
264 /**
265   @def mysql_file_ftell(FD)
266   Instrumented ftell.
267   @c mysql_file_ftell is a replacement for @c my_ftell.
268 */
269 #ifdef HAVE_PSI_FILE_INTERFACE
270 #define mysql_file_ftell(FD) inline_mysql_file_ftell(__FILE__, __LINE__, FD)
271 #else
272 #define mysql_file_ftell(FD) inline_mysql_file_ftell(FD)
273 #endif
274 
275 /**
276   @def mysql_file_create(K, N, F1, F2, F3)
277   Instrumented create.
278   @c mysql_file_create is a replacement for @c my_create.
279 */
280 #ifdef HAVE_PSI_FILE_INTERFACE
281 #define mysql_file_create(K, N, F1, F2, F3) \
282   inline_mysql_file_create(K, __FILE__, __LINE__, N, F1, F2, F3)
283 #else
284 #define mysql_file_create(K, N, F1, F2, F3) \
285   inline_mysql_file_create(N, F1, F2, F3)
286 #endif
287 
288 /**
289   @def mysql_file_create_temp(K, T, D, P, M, F)
290   Instrumented create_temp_file.
291   @c mysql_file_create_temp is a replacement for @c create_temp_file.
292 */
293 #ifdef HAVE_PSI_FILE_INTERFACE
294 #define mysql_file_create_temp(K, T, D, P, M, F) \
295   inline_mysql_file_create_temp(K, __FILE__, __LINE__, T, D, P, M, F)
296 #else
297 #define mysql_file_create_temp(K, T, D, P, M, F) \
298   inline_mysql_file_create_temp(T, D, P, M, F)
299 #endif
300 
301 /**
302   @def mysql_file_open(K, N, F1, F2)
303   Instrumented open.
304   @c mysql_file_open is a replacement for @c my_open.
305 */
306 #ifdef HAVE_PSI_FILE_INTERFACE
307 #define mysql_file_open(K, N, F1, F2) \
308   inline_mysql_file_open(K, __FILE__, __LINE__, N, F1, F2)
309 #else
310 #define mysql_file_open(K, N, F1, F2) inline_mysql_file_open(N, F1, F2)
311 #endif
312 
313 /**
314   @def mysql_file_close(FD, F)
315   Instrumented close.
316   @c mysql_file_close is a replacement for @c my_close.
317 */
318 #ifdef HAVE_PSI_FILE_INTERFACE
319 #define mysql_file_close(FD, F) \
320   inline_mysql_file_close(__FILE__, __LINE__, FD, F)
321 #else
322 #define mysql_file_close(FD, F) inline_mysql_file_close(FD, F)
323 #endif
324 
325 /**
326   @def mysql_file_read(FD, B, S, F)
327   Instrumented read.
328   @c mysql_read is a replacement for @c my_read.
329 */
330 #ifdef HAVE_PSI_FILE_INTERFACE
331 #define mysql_file_read(FD, B, S, F) \
332   inline_mysql_file_read(__FILE__, __LINE__, FD, B, S, F)
333 #else
334 #define mysql_file_read(FD, B, S, F) inline_mysql_file_read(FD, B, S, F)
335 #endif
336 
337 /**
338   @def mysql_file_write(FD, B, S, F)
339   Instrumented write.
340   @c mysql_file_write is a replacement for @c my_write.
341 */
342 #ifdef HAVE_PSI_FILE_INTERFACE
343 #define mysql_file_write(FD, B, S, F) \
344   inline_mysql_file_write(__FILE__, __LINE__, FD, B, S, F)
345 #else
346 #define mysql_file_write(FD, B, S, F) inline_mysql_file_write(FD, B, S, F)
347 #endif
348 
349 /**
350   @def mysql_file_pread(FD, B, S, O, F)
351   Instrumented pread.
352   @c mysql_pread is a replacement for @c my_pread.
353 */
354 #ifdef HAVE_PSI_FILE_INTERFACE
355 #define mysql_file_pread(FD, B, S, O, F) \
356   inline_mysql_file_pread(__FILE__, __LINE__, FD, B, S, O, F)
357 #else
358 #define mysql_file_pread(FD, B, S, O, F) inline_mysql_file_pread(FD, B, S, O, F)
359 #endif
360 
361 /**
362   @def mysql_file_pwrite(FD, B, S, O, F)
363   Instrumented pwrite.
364   @c mysql_file_pwrite is a replacement for @c my_pwrite.
365 */
366 #ifdef HAVE_PSI_FILE_INTERFACE
367 #define mysql_file_pwrite(FD, B, S, O, F) \
368   inline_mysql_file_pwrite(__FILE__, __LINE__, FD, B, S, O, F)
369 #else
370 #define mysql_file_pwrite(FD, B, S, O, F) \
371   inline_mysql_file_pwrite(FD, B, S, O, F)
372 #endif
373 
374 /**
375   @def mysql_file_seek(FD, P, W, F)
376   Instrumented seek.
377   @c mysql_file_seek is a replacement for @c my_seek.
378 */
379 #ifdef HAVE_PSI_FILE_INTERFACE
380 #define mysql_file_seek(FD, P, W, F) \
381   inline_mysql_file_seek(__FILE__, __LINE__, FD, P, W, F)
382 #else
383 #define mysql_file_seek(FD, P, W, F) inline_mysql_file_seek(FD, P, W, F)
384 #endif
385 
386 /**
387   @def mysql_file_tell(FD, F)
388   Instrumented tell.
389   @c mysql_file_tell is a replacement for @c my_tell.
390 */
391 #ifdef HAVE_PSI_FILE_INTERFACE
392 #define mysql_file_tell(FD, F) inline_mysql_file_tell(__FILE__, __LINE__, FD, F)
393 #else
394 #define mysql_file_tell(FD, F) inline_mysql_file_tell(FD, F)
395 #endif
396 
397 /**
398   @def mysql_file_delete(K, P1, P2)
399   Instrumented delete.
400   @c mysql_file_delete is a replacement for @c my_delete.
401 */
402 #ifdef HAVE_PSI_FILE_INTERFACE
403 #define mysql_file_delete(K, P1, P2) \
404   inline_mysql_file_delete(K, __FILE__, __LINE__, P1, P2)
405 #else
406 #define mysql_file_delete(K, P1, P2) inline_mysql_file_delete(P1, P2)
407 #endif
408 
409 /**
410   @def mysql_file_rename(K, P1, P2, P3)
411   Instrumented rename.
412   @c mysql_file_rename is a replacement for @c my_rename.
413 */
414 #ifdef HAVE_PSI_FILE_INTERFACE
415 #define mysql_file_rename(K, P1, P2, P3) \
416   inline_mysql_file_rename(K, __FILE__, __LINE__, P1, P2, P3)
417 #else
418 #define mysql_file_rename(K, P1, P2, P3) inline_mysql_file_rename(P1, P2, P3)
419 #endif
420 
421 /**
422   @def mysql_file_create_with_symlink(K, P1, P2, P3, P4, P5)
423   Instrumented create with symbolic link.
424   @c mysql_file_create_with_symlink is a replacement
425   for @c my_create_with_symlink.
426 */
427 #ifdef HAVE_PSI_FILE_INTERFACE
428 #define mysql_file_create_with_symlink(K, P1, P2, P3, P4, P5)                  \
429   inline_mysql_file_create_with_symlink(K, __FILE__, __LINE__, P1, P2, P3, P4, \
430                                         P5)
431 #else
432 #define mysql_file_create_with_symlink(K, P1, P2, P3, P4, P5) \
433   inline_mysql_file_create_with_symlink(P1, P2, P3, P4, P5)
434 #endif
435 
436 /**
437   @def mysql_file_delete_with_symlink(K, P1, P2)
438   Instrumented delete with symbolic link.
439   @c mysql_file_delete_with_symlink is a replacement
440   for @c my_delete_with_symlink.
441 */
442 #ifdef HAVE_PSI_FILE_INTERFACE
443 #define mysql_file_delete_with_symlink(K, P1, P2) \
444   inline_mysql_file_delete_with_symlink(K, __FILE__, __LINE__, P1, P2)
445 #else
446 #define mysql_file_delete_with_symlink(K, P1, P2) \
447   inline_mysql_file_delete_with_symlink(P1, P2)
448 #endif
449 
450 /**
451   @def mysql_file_rename_with_symlink(K, P1, P2, P3)
452   Instrumented rename with symbolic link.
453   @c mysql_file_rename_with_symlink is a replacement
454   for @c my_rename_with_symlink.
455 */
456 #ifdef HAVE_PSI_FILE_INTERFACE
457 #define mysql_file_rename_with_symlink(K, P1, P2, P3) \
458   inline_mysql_file_rename_with_symlink(K, __FILE__, __LINE__, P1, P2, P3)
459 #else
460 #define mysql_file_rename_with_symlink(K, P1, P2, P3) \
461   inline_mysql_file_rename_with_symlink(P1, P2, P3)
462 #endif
463 
464 /**
465   @def mysql_file_sync(P1, P2)
466   Instrumented file sync.
467   @c mysql_file_sync is a replacement for @c my_sync.
468 */
469 #ifdef HAVE_PSI_FILE_INTERFACE
470 #define mysql_file_sync(P1, P2) \
471   inline_mysql_file_sync(__FILE__, __LINE__, P1, P2)
472 #else
473 #define mysql_file_sync(P1, P2) inline_mysql_file_sync(P1, P2)
474 #endif
475 
476 /**
477   An instrumented FILE structure.
478   @c MYSQL_FILE is a drop-in replacement for @c FILE.
479   @sa mysql_file_open
480 */
481 struct MYSQL_FILE {
482   /** The real file. */
483   FILE *m_file;
484   /**
485     The instrumentation hook.
486     Note that this hook is not conditionally defined,
487     for binary compatibility of the @c MYSQL_FILE interface.
488   */
489   struct PSI_file *m_psi;
490 };
491 
inline_mysql_file_register(const char * category,PSI_file_info * info,int count)492 static inline void inline_mysql_file_register(
493 #ifdef HAVE_PSI_FILE_INTERFACE
494     const char *category, PSI_file_info *info, int count
495 #else
496     const char *category MY_ATTRIBUTE((unused)),
497     void *info MY_ATTRIBUTE((unused)), int count MY_ATTRIBUTE((unused))
498 #endif
499 ) {
500 #ifdef HAVE_PSI_FILE_INTERFACE
501   PSI_FILE_CALL(register_file)(category, info, count);
502 #endif
503 }
504 
inline_mysql_file_fgets(const char * src_file,uint src_line,char * str,int size,MYSQL_FILE * file)505 static inline char *inline_mysql_file_fgets(
506 #ifdef HAVE_PSI_FILE_INTERFACE
507     const char *src_file, uint src_line,
508 #endif
509     char *str, int size, MYSQL_FILE *file) {
510   char *result;
511 #ifdef HAVE_PSI_FILE_INTERFACE
512   struct PSI_file_locker *locker;
513   PSI_file_locker_state state;
514   locker = PSI_FILE_CALL(get_thread_file_stream_locker)(&state, file->m_psi,
515                                                         PSI_FILE_READ);
516   if (likely(locker != NULL)) {
517     PSI_FILE_CALL(start_file_wait)(locker, (size_t)size, src_file, src_line);
518     result = fgets(str, size, file->m_file);
519     PSI_FILE_CALL(end_file_wait)(locker, result ? strlen(result) : 0);
520     return result;
521   }
522 #endif
523 
524   result = fgets(str, size, file->m_file);
525   return result;
526 }
527 
inline_mysql_file_fgetc(const char * src_file,uint src_line,MYSQL_FILE * file)528 static inline int inline_mysql_file_fgetc(
529 #ifdef HAVE_PSI_FILE_INTERFACE
530     const char *src_file, uint src_line,
531 #endif
532     MYSQL_FILE *file) {
533   int result;
534 #ifdef HAVE_PSI_FILE_INTERFACE
535   struct PSI_file_locker *locker;
536   PSI_file_locker_state state;
537   locker = PSI_FILE_CALL(get_thread_file_stream_locker)(&state, file->m_psi,
538                                                         PSI_FILE_READ);
539   if (likely(locker != NULL)) {
540     PSI_FILE_CALL(start_file_wait)(locker, (size_t)1, src_file, src_line);
541     result = fgetc(file->m_file);
542     PSI_FILE_CALL(end_file_wait)(locker, (size_t)1);
543     return result;
544   }
545 #endif
546 
547   result = fgetc(file->m_file);
548   return result;
549 }
550 
inline_mysql_file_fputs(const char * src_file,uint src_line,const char * str,MYSQL_FILE * file)551 static inline int inline_mysql_file_fputs(
552 #ifdef HAVE_PSI_FILE_INTERFACE
553     const char *src_file, uint src_line,
554 #endif
555     const char *str, MYSQL_FILE *file) {
556   int result;
557 #ifdef HAVE_PSI_FILE_INTERFACE
558   struct PSI_file_locker *locker;
559   PSI_file_locker_state state;
560   size_t bytes;
561   locker = PSI_FILE_CALL(get_thread_file_stream_locker)(&state, file->m_psi,
562                                                         PSI_FILE_WRITE);
563   if (likely(locker != NULL)) {
564     bytes = str ? strlen(str) : 0;
565     PSI_FILE_CALL(start_file_wait)(locker, bytes, src_file, src_line);
566     result = fputs(str, file->m_file);
567     PSI_FILE_CALL(end_file_wait)(locker, bytes);
568     return result;
569   }
570 #endif
571 
572   result = fputs(str, file->m_file);
573   return result;
574 }
575 
inline_mysql_file_fputc(const char * src_file,uint src_line,char c,MYSQL_FILE * file)576 static inline int inline_mysql_file_fputc(
577 #ifdef HAVE_PSI_FILE_INTERFACE
578     const char *src_file, uint src_line,
579 #endif
580     char c, MYSQL_FILE *file) {
581   int result;
582 #ifdef HAVE_PSI_FILE_INTERFACE
583   struct PSI_file_locker *locker;
584   PSI_file_locker_state state;
585   locker = PSI_FILE_CALL(get_thread_file_stream_locker)(&state, file->m_psi,
586                                                         PSI_FILE_WRITE);
587   if (likely(locker != NULL)) {
588     PSI_FILE_CALL(start_file_wait)(locker, (size_t)1, src_file, src_line);
589     result = fputc(c, file->m_file);
590     PSI_FILE_CALL(end_file_wait)(locker, (size_t)1);
591     return result;
592   }
593 #endif
594 
595   result = fputc(c, file->m_file);
596   return result;
597 }
598 
599 static inline int inline_mysql_file_fprintf(MYSQL_FILE *file,
600                                             const char *format, ...)
601     MY_ATTRIBUTE((format(printf, 2, 3)));
602 
inline_mysql_file_fprintf(MYSQL_FILE * file,const char * format,...)603 static inline int inline_mysql_file_fprintf(MYSQL_FILE *file,
604                                             const char *format, ...) {
605   /*
606     TODO: figure out how to pass src_file and src_line from the caller.
607   */
608   int result;
609   va_list args;
610 #ifdef HAVE_PSI_FILE_INTERFACE
611   struct PSI_file_locker *locker;
612   PSI_file_locker_state state;
613   locker = PSI_FILE_CALL(get_thread_file_stream_locker)(&state, file->m_psi,
614                                                         PSI_FILE_WRITE);
615   if (likely(locker != NULL)) {
616     PSI_FILE_CALL(start_file_wait)(locker, (size_t)0, __FILE__, __LINE__);
617     va_start(args, format);
618     result = vfprintf(file->m_file, format, args);
619     va_end(args);
620     PSI_FILE_CALL(end_file_wait)(locker, (size_t)result);
621     return result;
622   }
623 #endif
624 
625   va_start(args, format);
626   result = vfprintf(file->m_file, format, args);
627   va_end(args);
628   return result;
629 }
630 
631 static inline int inline_mysql_file_vfprintf(
632 #ifdef HAVE_PSI_FILE_INTERFACE
633     const char *src_file, uint src_line,
634 #endif
635     MYSQL_FILE *file, const char *format, va_list args)
636 #ifdef HAVE_PSI_FILE_INTERFACE
637     MY_ATTRIBUTE((format(printf, 4, 0)));
638 #else
639     MY_ATTRIBUTE((format(printf, 2, 0)));
640 #endif
641 
inline_mysql_file_vfprintf(const char * src_file,uint src_line,MYSQL_FILE * file,const char * format,va_list args)642 static inline int inline_mysql_file_vfprintf(
643 #ifdef HAVE_PSI_FILE_INTERFACE
644     const char *src_file, uint src_line,
645 #endif
646     MYSQL_FILE *file, const char *format, va_list args) {
647   int result;
648 #ifdef HAVE_PSI_FILE_INTERFACE
649   struct PSI_file_locker *locker;
650   PSI_file_locker_state state;
651   locker = PSI_FILE_CALL(get_thread_file_stream_locker)(&state, file->m_psi,
652                                                         PSI_FILE_WRITE);
653   if (likely(locker != NULL)) {
654     PSI_FILE_CALL(start_file_wait)(locker, (size_t)0, src_file, src_line);
655     result = vfprintf(file->m_file, format, args);
656     PSI_FILE_CALL(end_file_wait)(locker, (size_t)result);
657     return result;
658   }
659 #endif
660 
661   result = vfprintf(file->m_file, format, args);
662   return result;
663 }
664 
inline_mysql_file_fflush(const char * src_file,uint src_line,MYSQL_FILE * file)665 static inline int inline_mysql_file_fflush(
666 #ifdef HAVE_PSI_FILE_INTERFACE
667     const char *src_file, uint src_line,
668 #endif
669     MYSQL_FILE *file) {
670   int result;
671 #ifdef HAVE_PSI_FILE_INTERFACE
672   struct PSI_file_locker *locker;
673   PSI_file_locker_state state;
674   locker = PSI_FILE_CALL(get_thread_file_stream_locker)(&state, file->m_psi,
675                                                         PSI_FILE_FLUSH);
676   if (likely(locker != NULL)) {
677     PSI_FILE_CALL(start_file_wait)(locker, (size_t)0, src_file, src_line);
678     result = fflush(file->m_file);
679     PSI_FILE_CALL(end_file_wait)(locker, (size_t)0);
680     return result;
681   }
682 #endif
683 
684   result = fflush(file->m_file);
685   return result;
686 }
687 
inline_mysql_file_feof(MYSQL_FILE * file)688 static inline int inline_mysql_file_feof(MYSQL_FILE *file) {
689   /* Not instrumented, there is no wait involved */
690   return feof(file->m_file);
691 }
692 
inline_mysql_file_fstat(const char * src_file,uint src_line,int filenr,MY_STAT * stat_area)693 static inline int inline_mysql_file_fstat(
694 #ifdef HAVE_PSI_FILE_INTERFACE
695     const char *src_file, uint src_line,
696 #endif
697     int filenr, MY_STAT *stat_area) {
698   int result;
699 #ifdef HAVE_PSI_FILE_INTERFACE
700   struct PSI_file_locker *locker;
701   PSI_file_locker_state state;
702   locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, filenr,
703                                                             PSI_FILE_FSTAT);
704   if (likely(locker != NULL)) {
705     PSI_FILE_CALL(start_file_wait)(locker, (size_t)0, src_file, src_line);
706     result = my_fstat(filenr, stat_area);
707     PSI_FILE_CALL(end_file_wait)(locker, (size_t)0);
708     return result;
709   }
710 #endif
711 
712   result = my_fstat(filenr, stat_area);
713   return result;
714 }
715 
inline_mysql_file_stat(PSI_file_key key,const char * src_file,uint src_line,const char * path,MY_STAT * stat_area,myf flags)716 static inline MY_STAT *inline_mysql_file_stat(
717 #ifdef HAVE_PSI_FILE_INTERFACE
718     PSI_file_key key, const char *src_file, uint src_line,
719 #endif
720     const char *path, MY_STAT *stat_area, myf flags) {
721   MY_STAT *result;
722 #ifdef HAVE_PSI_FILE_INTERFACE
723   struct PSI_file_locker *locker;
724   PSI_file_locker_state state;
725   locker = PSI_FILE_CALL(get_thread_file_name_locker)(
726       &state, key, PSI_FILE_STAT, path, &locker);
727   if (likely(locker != NULL)) {
728     PSI_FILE_CALL(start_file_open_wait)(locker, src_file, src_line);
729     result = my_stat(path, stat_area, flags);
730     PSI_FILE_CALL(end_file_open_wait)(locker, result);
731     return result;
732   }
733 #endif
734 
735   result = my_stat(path, stat_area, flags);
736   return result;
737 }
738 
inline_mysql_file_chsize(const char * src_file,uint src_line,File file,my_off_t newlength,int filler,myf flags)739 static inline int inline_mysql_file_chsize(
740 #ifdef HAVE_PSI_FILE_INTERFACE
741     const char *src_file, uint src_line,
742 #endif
743     File file, my_off_t newlength, int filler, myf flags) {
744   int result;
745 #ifdef HAVE_PSI_FILE_INTERFACE
746   struct PSI_file_locker *locker;
747   PSI_file_locker_state state;
748   locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, file,
749                                                             PSI_FILE_CHSIZE);
750   if (likely(locker != NULL)) {
751     PSI_FILE_CALL(start_file_wait)
752     (locker, (size_t)newlength, src_file, src_line);
753     result = my_chsize(file, newlength, filler, flags);
754     PSI_FILE_CALL(end_file_wait)(locker, (size_t)newlength);
755     return result;
756   }
757 #endif
758 
759   result = my_chsize(file, newlength, filler, flags);
760   return result;
761 }
762 
inline_mysql_file_fopen(PSI_file_key key,const char * src_file,uint src_line,const char * filename,int flags,myf myFlags)763 static inline MYSQL_FILE *inline_mysql_file_fopen(
764 #ifdef HAVE_PSI_FILE_INTERFACE
765     PSI_file_key key, const char *src_file, uint src_line,
766 #endif
767     const char *filename, int flags, myf myFlags) {
768   MYSQL_FILE *that;
769   that = (MYSQL_FILE *)my_malloc(PSI_NOT_INSTRUMENTED, sizeof(MYSQL_FILE),
770                                  MYF(MY_WME));
771   if (likely(that != NULL)) {
772 #ifdef HAVE_PSI_FILE_INTERFACE
773     struct PSI_file_locker *locker;
774     PSI_file_locker_state state;
775     locker = PSI_FILE_CALL(get_thread_file_name_locker)(
776         &state, key, PSI_FILE_STREAM_OPEN, filename, that);
777     if (likely(locker != NULL)) {
778       PSI_FILE_CALL(start_file_open_wait)
779       (locker, src_file, src_line);
780       that->m_file = my_fopen(filename, flags, myFlags);
781       that->m_psi = PSI_FILE_CALL(end_file_open_wait)(locker, that->m_file);
782       if (unlikely(that->m_file == NULL)) {
783         my_free(that);
784         return NULL;
785       }
786       return that;
787     }
788 #endif
789 
790     that->m_psi = NULL;
791     that->m_file = my_fopen(filename, flags, myFlags);
792     if (unlikely(that->m_file == NULL)) {
793       my_free(that);
794       return NULL;
795     }
796   }
797   return that;
798 }
799 
inline_mysql_file_fclose(const char * src_file,uint src_line,MYSQL_FILE * file,myf flags)800 static inline int inline_mysql_file_fclose(
801 #ifdef HAVE_PSI_FILE_INTERFACE
802     const char *src_file, uint src_line,
803 #endif
804     MYSQL_FILE *file, myf flags) {
805   int result = 0;
806   if (likely(file != NULL)) {
807 #ifdef HAVE_PSI_FILE_INTERFACE
808     struct PSI_file_locker *locker;
809     PSI_file_locker_state state;
810     locker = PSI_FILE_CALL(get_thread_file_stream_locker)(
811         &state, file->m_psi, PSI_FILE_STREAM_CLOSE);
812     if (likely(locker != NULL)) {
813       PSI_FILE_CALL(start_file_close_wait)(locker, src_file, src_line);
814       result = my_fclose(file->m_file, flags);
815       PSI_FILE_CALL(end_file_close_wait)(locker, result);
816       my_free(file);
817       return result;
818     }
819 #endif
820 
821     result = my_fclose(file->m_file, flags);
822     my_free(file);
823   }
824   return result;
825 }
826 
inline_mysql_file_fread(const char * src_file,uint src_line,MYSQL_FILE * file,uchar * buffer,size_t count,myf flags)827 static inline size_t inline_mysql_file_fread(
828 #ifdef HAVE_PSI_FILE_INTERFACE
829     const char *src_file, uint src_line,
830 #endif
831     MYSQL_FILE *file, uchar *buffer, size_t count, myf flags) {
832   size_t result;
833 #ifdef HAVE_PSI_FILE_INTERFACE
834   struct PSI_file_locker *locker;
835   PSI_file_locker_state state;
836   size_t bytes_read;
837   locker = PSI_FILE_CALL(get_thread_file_stream_locker)(&state, file->m_psi,
838                                                         PSI_FILE_READ);
839   if (likely(locker != NULL)) {
840     PSI_FILE_CALL(start_file_wait)(locker, count, src_file, src_line);
841     result = my_fread(file->m_file, buffer, count, flags);
842     if (flags & (MY_NABP | MY_FNABP)) {
843       bytes_read = (result == 0) ? count : 0;
844     } else {
845       bytes_read = (result != MY_FILE_ERROR) ? result : 0;
846     }
847     PSI_FILE_CALL(end_file_wait)(locker, bytes_read);
848     return result;
849   }
850 #endif
851 
852   result = my_fread(file->m_file, buffer, count, flags);
853   return result;
854 }
855 
inline_mysql_file_fwrite(const char * src_file,uint src_line,MYSQL_FILE * file,const uchar * buffer,size_t count,myf flags)856 static inline size_t inline_mysql_file_fwrite(
857 #ifdef HAVE_PSI_FILE_INTERFACE
858     const char *src_file, uint src_line,
859 #endif
860     MYSQL_FILE *file, const uchar *buffer, size_t count, myf flags) {
861   size_t result;
862 #ifdef HAVE_PSI_FILE_INTERFACE
863   struct PSI_file_locker *locker;
864   PSI_file_locker_state state;
865   size_t bytes_written;
866   locker = PSI_FILE_CALL(get_thread_file_stream_locker)(&state, file->m_psi,
867                                                         PSI_FILE_WRITE);
868   if (likely(locker != NULL)) {
869     PSI_FILE_CALL(start_file_wait)(locker, count, src_file, src_line);
870     result = my_fwrite(file->m_file, buffer, count, flags);
871     if (flags & (MY_NABP | MY_FNABP)) {
872       bytes_written = (result == 0) ? count : 0;
873     } else {
874       bytes_written = (result != MY_FILE_ERROR) ? result : 0;
875     }
876     PSI_FILE_CALL(end_file_wait)(locker, bytes_written);
877     return result;
878   }
879 #endif
880 
881   result = my_fwrite(file->m_file, buffer, count, flags);
882   return result;
883 }
884 
inline_mysql_file_fseek(const char * src_file,uint src_line,MYSQL_FILE * file,my_off_t pos,int whence)885 static inline my_off_t inline_mysql_file_fseek(
886 #ifdef HAVE_PSI_FILE_INTERFACE
887     const char *src_file, uint src_line,
888 #endif
889     MYSQL_FILE *file, my_off_t pos, int whence) {
890   my_off_t result;
891 #ifdef HAVE_PSI_FILE_INTERFACE
892   struct PSI_file_locker *locker;
893   PSI_file_locker_state state;
894   locker = PSI_FILE_CALL(get_thread_file_stream_locker)(&state, file->m_psi,
895                                                         PSI_FILE_SEEK);
896   if (likely(locker != NULL)) {
897     PSI_FILE_CALL(start_file_wait)(locker, (size_t)0, src_file, src_line);
898     result = my_fseek(file->m_file, pos, whence);
899     PSI_FILE_CALL(end_file_wait)(locker, (size_t)0);
900     return result;
901   }
902 #endif
903 
904   result = my_fseek(file->m_file, pos, whence);
905   return result;
906 }
907 
inline_mysql_file_ftell(const char * src_file,uint src_line,MYSQL_FILE * file)908 static inline my_off_t inline_mysql_file_ftell(
909 #ifdef HAVE_PSI_FILE_INTERFACE
910     const char *src_file, uint src_line,
911 #endif
912     MYSQL_FILE *file) {
913   my_off_t result;
914 #ifdef HAVE_PSI_FILE_INTERFACE
915   struct PSI_file_locker *locker;
916   PSI_file_locker_state state;
917   locker = PSI_FILE_CALL(get_thread_file_stream_locker)(&state, file->m_psi,
918                                                         PSI_FILE_TELL);
919   if (likely(locker != NULL)) {
920     PSI_FILE_CALL(start_file_wait)(locker, (size_t)0, src_file, src_line);
921     result = my_ftell(file->m_file);
922     PSI_FILE_CALL(end_file_wait)(locker, (size_t)0);
923     return result;
924   }
925 #endif
926 
927   result = my_ftell(file->m_file);
928   return result;
929 }
930 
inline_mysql_file_create(PSI_file_key key,const char * src_file,uint src_line,const char * filename,int create_flags,int access_flags,myf myFlags)931 static inline File inline_mysql_file_create(
932 #ifdef HAVE_PSI_FILE_INTERFACE
933     PSI_file_key key, const char *src_file, uint src_line,
934 #endif
935     const char *filename, int create_flags, int access_flags, myf myFlags) {
936   File file;
937 #ifdef HAVE_PSI_FILE_INTERFACE
938   struct PSI_file_locker *locker;
939   PSI_file_locker_state state;
940   locker = PSI_FILE_CALL(get_thread_file_name_locker)(
941       &state, key, PSI_FILE_CREATE, filename, &locker);
942   if (likely(locker != NULL)) {
943     PSI_FILE_CALL(start_file_open_wait)(locker, src_file, src_line);
944     file = my_create(filename, create_flags, access_flags, myFlags);
945     PSI_FILE_CALL(end_file_open_wait_and_bind_to_descriptor)(locker, file);
946     return file;
947   }
948 #endif
949 
950   file = my_create(filename, create_flags, access_flags, myFlags);
951   return file;
952 }
953 
inline_mysql_file_create_temp(PSI_file_key key,const char * src_file,uint src_line,char * to,const char * dir,const char * pfx,int mode,myf myFlags)954 static inline File inline_mysql_file_create_temp(
955 #ifdef HAVE_PSI_FILE_INTERFACE
956     PSI_file_key key, const char *src_file, uint src_line,
957 #endif
958     char *to, const char *dir, const char *pfx, int mode, myf myFlags) {
959   File file;
960 #ifdef HAVE_PSI_FILE_INTERFACE
961   struct PSI_file_locker *locker;
962   PSI_file_locker_state state;
963   locker = PSI_FILE_CALL(get_thread_file_name_locker)(
964       &state, key, PSI_FILE_CREATE, NULL, &locker);
965   if (likely(locker != NULL)) {
966     PSI_FILE_CALL(start_file_open_wait)(locker, src_file, src_line);
967     /* The file name is generated by create_temp_file(). */
968     file = create_temp_file(to, dir, pfx, mode, myFlags);
969     PSI_FILE_CALL(end_temp_file_open_wait_and_bind_to_descriptor)
970     (locker, file, (const char *)to);
971     return file;
972   }
973 #endif
974 
975   file = create_temp_file(to, dir, pfx, mode, myFlags);
976   return file;
977 }
978 
inline_mysql_file_open(PSI_file_key key,const char * src_file,uint src_line,const char * filename,int flags,myf myFlags)979 static inline File inline_mysql_file_open(
980 #ifdef HAVE_PSI_FILE_INTERFACE
981     PSI_file_key key, const char *src_file, uint src_line,
982 #endif
983     const char *filename, int flags, myf myFlags) {
984   File file;
985 #ifdef HAVE_PSI_FILE_INTERFACE
986   struct PSI_file_locker *locker;
987   PSI_file_locker_state state;
988   locker = PSI_FILE_CALL(get_thread_file_name_locker)(
989       &state, key, PSI_FILE_OPEN, filename, &locker);
990   if (likely(locker != NULL)) {
991     PSI_FILE_CALL(start_file_open_wait)(locker, src_file, src_line);
992     file = my_open(filename, flags, myFlags);
993     PSI_FILE_CALL(end_file_open_wait_and_bind_to_descriptor)(locker, file);
994     return file;
995   }
996 #endif
997 
998   file = my_open(filename, flags, myFlags);
999   return file;
1000 }
1001 
inline_mysql_file_close(const char * src_file,uint src_line,File file,myf flags)1002 static inline int inline_mysql_file_close(
1003 #ifdef HAVE_PSI_FILE_INTERFACE
1004     const char *src_file, uint src_line,
1005 #endif
1006     File file, myf flags) {
1007   int result;
1008 #ifdef HAVE_PSI_FILE_INTERFACE
1009   struct PSI_file_locker *locker;
1010   PSI_file_locker_state state;
1011   locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, file,
1012                                                             PSI_FILE_CLOSE);
1013   if (likely(locker != NULL)) {
1014     PSI_FILE_CALL(start_file_close_wait)(locker, src_file, src_line);
1015     result = my_close(file, flags);
1016     PSI_FILE_CALL(end_file_close_wait)(locker, result);
1017     return result;
1018   }
1019 #endif
1020 
1021   result = my_close(file, flags);
1022   return result;
1023 }
1024 
inline_mysql_file_read(const char * src_file,uint src_line,File file,uchar * buffer,size_t count,myf flags)1025 static inline size_t inline_mysql_file_read(
1026 #ifdef HAVE_PSI_FILE_INTERFACE
1027     const char *src_file, uint src_line,
1028 #endif
1029     File file, uchar *buffer, size_t count, myf flags) {
1030   size_t result;
1031 #ifdef HAVE_PSI_FILE_INTERFACE
1032   struct PSI_file_locker *locker;
1033   PSI_file_locker_state state;
1034   size_t bytes_read;
1035   locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, file,
1036                                                             PSI_FILE_READ);
1037   if (likely(locker != NULL)) {
1038     PSI_FILE_CALL(start_file_wait)(locker, count, src_file, src_line);
1039     result = my_read(file, buffer, count, flags);
1040     if (flags & (MY_NABP | MY_FNABP)) {
1041       bytes_read = (result == 0) ? count : 0;
1042     } else {
1043       bytes_read = (result != MY_FILE_ERROR) ? result : 0;
1044     }
1045     PSI_FILE_CALL(end_file_wait)(locker, bytes_read);
1046     return result;
1047   }
1048 #endif
1049 
1050   result = my_read(file, buffer, count, flags);
1051   return result;
1052 }
1053 
inline_mysql_file_write(const char * src_file,uint src_line,File file,const uchar * buffer,size_t count,myf flags)1054 static inline size_t inline_mysql_file_write(
1055 #ifdef HAVE_PSI_FILE_INTERFACE
1056     const char *src_file, uint src_line,
1057 #endif
1058     File file, const uchar *buffer, size_t count, myf flags) {
1059   size_t result;
1060 #ifdef HAVE_PSI_FILE_INTERFACE
1061   struct PSI_file_locker *locker;
1062   PSI_file_locker_state state;
1063   size_t bytes_written;
1064   locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, file,
1065                                                             PSI_FILE_WRITE);
1066   if (likely(locker != NULL)) {
1067     PSI_FILE_CALL(start_file_wait)(locker, count, src_file, src_line);
1068     result = my_write(file, buffer, count, flags);
1069     if (flags & (MY_NABP | MY_FNABP)) {
1070       bytes_written = (result == 0) ? count : 0;
1071     } else {
1072       bytes_written = (result != MY_FILE_ERROR) ? result : 0;
1073     }
1074     PSI_FILE_CALL(end_file_wait)(locker, bytes_written);
1075     return result;
1076   }
1077 #endif
1078 
1079   result = my_write(file, buffer, count, flags);
1080   return result;
1081 }
1082 
inline_mysql_file_pread(const char * src_file,uint src_line,File file,uchar * buffer,size_t count,my_off_t offset,myf flags)1083 static inline size_t inline_mysql_file_pread(
1084 #ifdef HAVE_PSI_FILE_INTERFACE
1085     const char *src_file, uint src_line,
1086 #endif
1087     File file, uchar *buffer, size_t count, my_off_t offset, myf flags) {
1088   size_t result;
1089 #ifdef HAVE_PSI_FILE_INTERFACE
1090   struct PSI_file_locker *locker;
1091   PSI_file_locker_state state;
1092   size_t bytes_read;
1093   locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, file,
1094                                                             PSI_FILE_READ);
1095   if (likely(locker != NULL)) {
1096     PSI_FILE_CALL(start_file_wait)(locker, count, src_file, src_line);
1097     result = my_pread(file, buffer, count, offset, flags);
1098     if (flags & (MY_NABP | MY_FNABP)) {
1099       bytes_read = (result == 0) ? count : 0;
1100     } else {
1101       bytes_read = (result != MY_FILE_ERROR) ? result : 0;
1102     }
1103     PSI_FILE_CALL(end_file_wait)(locker, bytes_read);
1104     return result;
1105   }
1106 #endif
1107 
1108   result = my_pread(file, buffer, count, offset, flags);
1109   return result;
1110 }
1111 
inline_mysql_file_pwrite(const char * src_file,uint src_line,File file,const uchar * buffer,size_t count,my_off_t offset,myf flags)1112 static inline size_t inline_mysql_file_pwrite(
1113 #ifdef HAVE_PSI_FILE_INTERFACE
1114     const char *src_file, uint src_line,
1115 #endif
1116     File file, const uchar *buffer, size_t count, my_off_t offset, myf flags) {
1117   size_t result;
1118 #ifdef HAVE_PSI_FILE_INTERFACE
1119   struct PSI_file_locker *locker;
1120   PSI_file_locker_state state;
1121   size_t bytes_written;
1122   locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, file,
1123                                                             PSI_FILE_WRITE);
1124   if (likely(locker != NULL)) {
1125     PSI_FILE_CALL(start_file_wait)(locker, count, src_file, src_line);
1126     result = my_pwrite(file, buffer, count, offset, flags);
1127     if (flags & (MY_NABP | MY_FNABP)) {
1128       bytes_written = (result == 0) ? count : 0;
1129     } else {
1130       bytes_written = (result != MY_FILE_ERROR) ? result : 0;
1131     }
1132     PSI_FILE_CALL(end_file_wait)(locker, bytes_written);
1133     return result;
1134   }
1135 #endif
1136 
1137   result = my_pwrite(file, buffer, count, offset, flags);
1138   return result;
1139 }
1140 
inline_mysql_file_seek(const char * src_file,uint src_line,File file,my_off_t pos,int whence,myf flags)1141 static inline my_off_t inline_mysql_file_seek(
1142 #ifdef HAVE_PSI_FILE_INTERFACE
1143     const char *src_file, uint src_line,
1144 #endif
1145     File file, my_off_t pos, int whence, myf flags) {
1146   my_off_t result;
1147 #ifdef HAVE_PSI_FILE_INTERFACE
1148   struct PSI_file_locker *locker;
1149   PSI_file_locker_state state;
1150   locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, file,
1151                                                             PSI_FILE_SEEK);
1152   if (likely(locker != NULL)) {
1153     PSI_FILE_CALL(start_file_wait)(locker, (size_t)0, src_file, src_line);
1154     result = my_seek(file, pos, whence, flags);
1155     PSI_FILE_CALL(end_file_wait)(locker, (size_t)0);
1156     return result;
1157   }
1158 #endif
1159 
1160   result = my_seek(file, pos, whence, flags);
1161   return result;
1162 }
1163 
inline_mysql_file_tell(const char * src_file,uint src_line,File file,myf flags)1164 static inline my_off_t inline_mysql_file_tell(
1165 #ifdef HAVE_PSI_FILE_INTERFACE
1166     const char *src_file, uint src_line,
1167 #endif
1168     File file, myf flags) {
1169   my_off_t result;
1170 #ifdef HAVE_PSI_FILE_INTERFACE
1171   struct PSI_file_locker *locker;
1172   PSI_file_locker_state state;
1173   locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, file,
1174                                                             PSI_FILE_TELL);
1175   if (likely(locker != NULL)) {
1176     PSI_FILE_CALL(start_file_wait)(locker, (size_t)0, src_file, src_line);
1177     result = my_tell(file, flags);
1178     PSI_FILE_CALL(end_file_wait)(locker, (size_t)0);
1179     return result;
1180   }
1181 #endif
1182 
1183   result = my_tell(file, flags);
1184   return result;
1185 }
1186 
inline_mysql_file_delete(PSI_file_key key,const char * src_file,uint src_line,const char * name,myf flags)1187 static inline int inline_mysql_file_delete(
1188 #ifdef HAVE_PSI_FILE_INTERFACE
1189     PSI_file_key key, const char *src_file, uint src_line,
1190 #endif
1191     const char *name, myf flags) {
1192   int result;
1193 #ifdef HAVE_PSI_FILE_INTERFACE
1194   struct PSI_file_locker *locker;
1195   PSI_file_locker_state state;
1196   locker = PSI_FILE_CALL(get_thread_file_name_locker)(
1197       &state, key, PSI_FILE_DELETE, name, &locker);
1198   if (likely(locker != NULL)) {
1199     PSI_FILE_CALL(start_file_close_wait)(locker, src_file, src_line);
1200     result = my_delete(name, flags);
1201     PSI_FILE_CALL(end_file_close_wait)(locker, result);
1202     return result;
1203   }
1204 #endif
1205 
1206   result = my_delete(name, flags);
1207   return result;
1208 }
1209 
inline_mysql_file_rename(PSI_file_key key,const char * src_file,uint src_line,const char * from,const char * to,myf flags)1210 static inline int inline_mysql_file_rename(
1211 #ifdef HAVE_PSI_FILE_INTERFACE
1212     PSI_file_key key, const char *src_file, uint src_line,
1213 #endif
1214     const char *from, const char *to, myf flags) {
1215   int result;
1216 #ifdef HAVE_PSI_FILE_INTERFACE
1217   struct PSI_file_locker *locker;
1218   PSI_file_locker_state state;
1219   locker = PSI_FILE_CALL(get_thread_file_name_locker)(
1220       &state, key, PSI_FILE_RENAME, from, &locker);
1221   if (likely(locker != NULL)) {
1222     PSI_FILE_CALL(start_file_wait)(locker, (size_t)0, src_file, src_line);
1223     result = my_rename(from, to, flags);
1224     PSI_FILE_CALL(end_file_rename_wait)(locker, from, to, result);
1225     return result;
1226   }
1227 #endif
1228 
1229   result = my_rename(from, to, flags);
1230   return result;
1231 }
1232 
inline_mysql_file_create_with_symlink(PSI_file_key key,const char * src_file,uint src_line,const char * linkname,const char * filename,int create_flags,int access_flags,myf flags)1233 static inline File inline_mysql_file_create_with_symlink(
1234 #ifdef HAVE_PSI_FILE_INTERFACE
1235     PSI_file_key key, const char *src_file, uint src_line,
1236 #endif
1237     const char *linkname, const char *filename, int create_flags,
1238     int access_flags, myf flags) {
1239   File file;
1240 #ifdef HAVE_PSI_FILE_INTERFACE
1241   struct PSI_file_locker *locker;
1242   PSI_file_locker_state state;
1243   locker = PSI_FILE_CALL(get_thread_file_name_locker)(
1244       &state, key, PSI_FILE_CREATE, filename, &locker);
1245   if (likely(locker != NULL)) {
1246     PSI_FILE_CALL(start_file_open_wait)(locker, src_file, src_line);
1247     file = my_create_with_symlink(linkname, filename, create_flags,
1248                                   access_flags, flags);
1249     PSI_FILE_CALL(end_file_open_wait_and_bind_to_descriptor)(locker, file);
1250     return file;
1251   }
1252 #endif
1253 
1254   file = my_create_with_symlink(linkname, filename, create_flags, access_flags,
1255                                 flags);
1256   return file;
1257 }
1258 
inline_mysql_file_delete_with_symlink(PSI_file_key key,const char * src_file,uint src_line,const char * name,myf flags)1259 static inline int inline_mysql_file_delete_with_symlink(
1260 #ifdef HAVE_PSI_FILE_INTERFACE
1261     PSI_file_key key, const char *src_file, uint src_line,
1262 #endif
1263     const char *name, myf flags) {
1264   int result;
1265 #ifdef HAVE_PSI_FILE_INTERFACE
1266   struct PSI_file_locker *locker;
1267   PSI_file_locker_state state;
1268   locker = PSI_FILE_CALL(get_thread_file_name_locker)(
1269       &state, key, PSI_FILE_DELETE, name, &locker);
1270   if (likely(locker != NULL)) {
1271     PSI_FILE_CALL(start_file_close_wait)(locker, src_file, src_line);
1272     result = my_delete_with_symlink(name, flags);
1273     PSI_FILE_CALL(end_file_close_wait)(locker, result);
1274     return result;
1275   }
1276 #endif
1277 
1278   result = my_delete_with_symlink(name, flags);
1279   return result;
1280 }
1281 
inline_mysql_file_rename_with_symlink(PSI_file_key key,const char * src_file,uint src_line,const char * from,const char * to,myf flags)1282 static inline int inline_mysql_file_rename_with_symlink(
1283 #ifdef HAVE_PSI_FILE_INTERFACE
1284     PSI_file_key key, const char *src_file, uint src_line,
1285 #endif
1286     const char *from, const char *to, myf flags) {
1287   int result;
1288 #ifdef HAVE_PSI_FILE_INTERFACE
1289   struct PSI_file_locker *locker;
1290   PSI_file_locker_state state;
1291   locker = PSI_FILE_CALL(get_thread_file_name_locker)(
1292       &state, key, PSI_FILE_RENAME, from, &locker);
1293   if (likely(locker != NULL)) {
1294     PSI_FILE_CALL(start_file_wait)(locker, (size_t)0, src_file, src_line);
1295     result = my_rename_with_symlink(from, to, flags);
1296     PSI_FILE_CALL(end_file_rename_wait)(locker, from, to, result);
1297     return result;
1298   }
1299 #endif
1300 
1301   result = my_rename_with_symlink(from, to, flags);
1302   return result;
1303 }
1304 
inline_mysql_file_sync(const char * src_file,uint src_line,File fd,myf flags)1305 static inline int inline_mysql_file_sync(
1306 #ifdef HAVE_PSI_FILE_INTERFACE
1307     const char *src_file, uint src_line,
1308 #endif
1309     File fd, myf flags) {
1310   int result = 0;
1311 #ifdef HAVE_PSI_FILE_INTERFACE
1312   struct PSI_file_locker *locker;
1313   PSI_file_locker_state state;
1314   locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, fd,
1315                                                             PSI_FILE_SYNC);
1316   if (likely(locker != NULL)) {
1317     PSI_FILE_CALL(start_file_wait)(locker, (size_t)0, src_file, src_line);
1318     result = my_sync(fd, flags);
1319     PSI_FILE_CALL(end_file_wait)(locker, (size_t)0);
1320     return result;
1321   }
1322 #endif
1323 
1324   result = my_sync(fd, flags);
1325   return result;
1326 }
1327 
1328   /** @} (end of group psi_api_file) */
1329 
1330 #endif
1331