mkvol_paral.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Author: Artem B. Bityutskiy
  19. *
  20. * This test creates and deletes volumes in parallel.
  21. */
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <pthread.h>
  26. #include "libubi.h"
  27. #define PROGRAM_NAME "mkvol_paral"
  28. #include "common.h"
  29. #include "helpers.h"
  30. #define THREADS_NUM 4
  31. #define ITERATIONS 500
  32. static libubi_t libubi;
  33. static struct ubi_dev_info dev_info;
  34. const char *node;
  35. static int iterations = ITERATIONS;
  36. /**
  37. * the_thread - the testing thread.
  38. *
  39. * @ptr thread number
  40. */
  41. static void * the_thread(void *ptr)
  42. {
  43. int n = (long)ptr, iter = iterations;
  44. struct ubi_mkvol_request req;
  45. const char *name = PROGRAM_NAME ":the_thread()";
  46. char nm[strlen(name) + 50];
  47. req.alignment = 1;
  48. req.bytes = dev_info.avail_bytes/ITERATIONS;
  49. req.vol_type = UBI_DYNAMIC_VOLUME;
  50. sprintf(nm, "%s:%d", name, n);
  51. req.name = nm;
  52. req.flags = 0;
  53. while (iter--) {
  54. req.vol_id = UBI_VOL_NUM_AUTO;
  55. if (ubi_mkvol(libubi, node, &req)) {
  56. failed("ubi_mkvol");
  57. return NULL;
  58. }
  59. if (ubi_rmvol(libubi, node, req.vol_id)) {
  60. failed("ubi_rmvol");
  61. return NULL;
  62. }
  63. }
  64. return NULL;
  65. }
  66. int main(int argc, char * const argv[])
  67. {
  68. int i, ret;
  69. pthread_t threads[THREADS_NUM];
  70. if (initial_check(argc, argv))
  71. return 1;
  72. node = argv[1];
  73. libubi = libubi_open();
  74. if (libubi == NULL) {
  75. failed("libubi_open");
  76. return 1;
  77. }
  78. if (ubi_get_dev_info(libubi, node, &dev_info)) {
  79. failed("ubi_get_dev_info");
  80. goto close;
  81. }
  82. for (i = 0; i < THREADS_NUM; i++) {
  83. ret = pthread_create(&threads[i], NULL, &the_thread, (void*)(long)i);
  84. if (ret) {
  85. failed("pthread_create");
  86. goto close;
  87. }
  88. }
  89. for (i = 0; i < THREADS_NUM; i++)
  90. pthread_join(threads[i], NULL);
  91. libubi_close(libubi);
  92. return 0;
  93. close:
  94. libubi_close(libubi);
  95. return 1;
  96. }