sync_stress_merge.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * sync stress test: merging
  3. * Copyright 2015-2016 Collabora Ltd.
  4. *
  5. * Based on the implementation from the Android Open Source Project,
  6. *
  7. * Copyright 2012 Google, Inc
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <time.h>
  30. #include "sync.h"
  31. #include "sw_sync.h"
  32. #include "synctest.h"
  33. int test_merge_stress_random_merge(void)
  34. {
  35. int i, size, ret;
  36. int timeline_count = 32;
  37. int merge_count = 1024 * 32;
  38. int timelines[timeline_count];
  39. int fence_map[timeline_count];
  40. int fence, tmpfence, merged, valid;
  41. int timeline, timeline_offset, sync_point;
  42. srand(time(NULL));
  43. for (i = 0; i < timeline_count; i++)
  44. timelines[i] = sw_sync_timeline_create();
  45. fence = sw_sync_fence_create(timelines[0], "fence", 0);
  46. valid = sw_sync_fence_is_valid(fence);
  47. ASSERT(valid, "Failure creating fence\n");
  48. memset(fence_map, -1, sizeof(fence_map));
  49. fence_map[0] = 0;
  50. /*
  51. * Randomly create sync_points out of a fixed set of timelines,
  52. * and merge them together
  53. */
  54. for (i = 0; i < merge_count; i++) {
  55. /* Generate sync_point. */
  56. timeline_offset = rand() % timeline_count;
  57. timeline = timelines[timeline_offset];
  58. sync_point = rand();
  59. /* Keep track of the latest sync_point in each timeline. */
  60. if (fence_map[timeline_offset] == -1)
  61. fence_map[timeline_offset] = sync_point;
  62. else if (fence_map[timeline_offset] < sync_point)
  63. fence_map[timeline_offset] = sync_point;
  64. /* Merge */
  65. tmpfence = sw_sync_fence_create(timeline, "fence", sync_point);
  66. merged = sync_merge("merge", tmpfence, fence);
  67. sw_sync_fence_destroy(tmpfence);
  68. sw_sync_fence_destroy(fence);
  69. fence = merged;
  70. valid = sw_sync_fence_is_valid(merged);
  71. ASSERT(valid, "Failure creating fence i\n");
  72. }
  73. size = 0;
  74. for (i = 0; i < timeline_count; i++)
  75. if (fence_map[i] != -1)
  76. size++;
  77. /* Confirm our map matches the fence. */
  78. ASSERT(sync_fence_size(fence) == size,
  79. "Quantity of elements not matching\n");
  80. /* Trigger the merged fence */
  81. for (i = 0; i < timeline_count; i++) {
  82. if (fence_map[i] != -1) {
  83. ret = sync_wait(fence, 0);
  84. ASSERT(ret == 0,
  85. "Failure waiting on fence until timeout\n");
  86. /* Increment the timeline to the last sync_point */
  87. sw_sync_timeline_inc(timelines[i], fence_map[i]);
  88. }
  89. }
  90. /* Check that the fence is triggered. */
  91. ret = sync_wait(fence, 0);
  92. ASSERT(ret > 0, "Failure triggering fence\n");
  93. sw_sync_fence_destroy(fence);
  94. for (i = 0; i < timeline_count; i++)
  95. sw_sync_timeline_destroy(timelines[i]);
  96. return 0;
  97. }