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