LCOV - code coverage report
Current view: top level - autotest/cpp - testblockcachewrite.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 83 85 97.6 %
Date: 2024-05-14 13:00:50 Functions: 13 14 92.9 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  * $Id$
       3             :  *
       4             :  * Project:  GDAL Core
       5             :  * Purpose:  Test block cache & writing behaviour under multi-threading
       6             :  * Author:   Even Rouault, <even dot rouault at spatialys dot com>
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2015, Even Rouault <even dot rouault at spatialys dot com>
      10             :  *
      11             :  * Permission is hereby granted, free of charge, to any person obtaining a
      12             :  * copy of this software and associated documentation files (the "Software"),
      13             :  * to deal in the Software without restriction, including without limitation
      14             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      15             :  * and/or sell copies of the Software, and to permit persons to whom the
      16             :  * Software is furnished to do so, subject to the following conditions:
      17             :  *
      18             :  * The above copyright notice and this permission notice shall be included
      19             :  * in all copies or substantial portions of the Software.
      20             :  *
      21             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      22             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      23             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      24             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      25             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      26             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      27             :  * DEALINGS IN THE SOFTWARE.
      28             :  ****************************************************************************/
      29             : 
      30             : #include "cpl_conv.h"
      31             : #include "cpl_multiproc.h"
      32             : #include "cpl_string.h"
      33             : #include "gdal_priv.h"
      34             : #include "test_data.h"
      35             : 
      36             : #include "gtest_include.h"
      37             : 
      38             : namespace
      39             : {
      40             : 
      41             : // ---------------------------------------------------------------------------
      42             : 
      43             : class MyRasterBand : public GDALRasterBand
      44             : {
      45             :     int bBusy;
      46             : 
      47             :   public:
      48           1 :     MyRasterBand()
      49           1 :     {
      50           1 :         nBlockXSize = 1;
      51           1 :         nBlockYSize = 1;
      52           1 :         bBusy = FALSE;
      53           1 :     }
      54             : 
      55           0 :     CPLErr IReadBlock(int, int, void *) CPL_OVERRIDE
      56             :     {
      57           0 :         CPLAssert(FALSE);
      58             :         return CE_Failure;
      59             :     }
      60             : 
      61           2 :     CPLErr IWriteBlock(int nXBlock, int nYBlock, void *) CPL_OVERRIDE
      62             :     {
      63           2 :         printf("Entering IWriteBlock(%d, %d)\n", nXBlock, nYBlock);
      64           2 :         EXPECT_TRUE(!bBusy);
      65           2 :         bBusy = TRUE;
      66           2 :         CPLSleep(0.5);
      67           2 :         bBusy = FALSE;
      68           2 :         printf("Leaving IWriteBlock(%d, %d)\n", nXBlock, nYBlock);
      69           2 :         return CE_None;
      70             :     }
      71             : };
      72             : 
      73             : class MyDataset : public GDALDataset
      74             : {
      75             :   public:
      76           1 :     MyDataset()
      77           1 :     {
      78           1 :         eAccess = GA_Update;
      79           1 :         nRasterXSize = 2;
      80           1 :         nRasterYSize = 1;
      81           1 :         SetBand(1, new MyRasterBand());
      82           1 :     }
      83             : 
      84           2 :     ~MyDataset()
      85           1 :     {
      86           1 :         FlushCache(true);
      87           2 :     }
      88             : };
      89             : 
      90           1 : static void thread_func1(void * /* unused */)
      91             : {
      92           1 :     printf("begin thread\n");
      93           1 :     GDALFlushCacheBlock();
      94           1 :     printf("end of thread\n\n");
      95           1 : }
      96             : 
      97           1 : static void test1()
      98             : {
      99             :     CPLJoinableThread *hThread;
     100             : 
     101           1 :     printf("Start test1\n");
     102           1 :     printf("main thread %p\n", (void *)CPLGetPID());
     103             : 
     104           1 :     GDALSetCacheMax(0);
     105             : 
     106           1 :     MyDataset *poDS = new MyDataset();
     107             : 
     108           1 :     char buf1[] = {1};
     109           1 :     CPL_IGNORE_RET_VAL(GDALRasterIO(GDALGetRasterBand(poDS, 1), GF_Write, 0, 0,
     110             :                                     1, 1, buf1, 1, 1, GDT_Byte, 0, 0));
     111             : 
     112           1 :     hThread = CPLCreateJoinableThread(thread_func1, nullptr);
     113           1 :     CPLSleep(0.3);
     114           1 :     CPL_IGNORE_RET_VAL(GDALRasterIO(GDALGetRasterBand(poDS, 1), GF_Write, 1, 0,
     115             :                                     1, 1, buf1, 1, 1, GDT_Byte, 0, 0));
     116           1 :     GDALFlushCacheBlock();
     117             : 
     118           1 :     CPLJoinThread(hThread);
     119             : 
     120           1 :     delete poDS;
     121           1 :     printf("End test1\n");
     122           1 : }
     123             : 
     124           1 : static void thread_func2(void * /* unused */)
     125             : {
     126           1 :     printf("begin thread %p\n", (void *)CPLGetPID());
     127           1 :     GDALDatasetH hDS = GDALOpen(TUT_ROOT_DATA_DIR "/byte.tif", GA_ReadOnly);
     128           1 :     GByte c = 0;
     129           1 :     CPL_IGNORE_RET_VAL(GDALDataset::FromHandle(hDS)->GetRasterBand(1)->RasterIO(
     130             :         GF_Read, 0, 0, 1, 1, &c, 1, 1, GDT_Byte, 0, 0, nullptr));
     131           1 :     GDALClose(hDS);
     132           1 :     printf("end of thread\n\n");
     133           1 : }
     134             : 
     135           1 : static void test2()
     136             : {
     137           1 :     printf("Start test2\n");
     138           1 :     printf("main thread %p\n", (void *)CPLGetPID());
     139             : 
     140             :     CPLJoinableThread *hThread;
     141             : 
     142           1 :     CPLSetConfigOption("GDAL_RB_INTERNALIZE_SLEEP_AFTER_DETACH_BEFORE_WRITE",
     143             :                        "0.5");
     144           1 :     GDALSetCacheMax(1000 * 1000);
     145             : 
     146           1 :     auto poDS = GetGDALDriverManager()->GetDriverByName("GTiff")->Create(
     147             :         "/vsimem/foo.tif", 1, 1, 2, GDT_Byte, nullptr);
     148           1 :     poDS->GetRasterBand(1)->Fill(0);
     149           1 :     poDS->GetRasterBand(2)->Fill(0);
     150           1 :     poDS->FlushCache(false);
     151           1 :     GDALSetCacheMax(0);
     152             : 
     153           1 :     poDS->GetRasterBand(1)->Fill(1);
     154           1 :     hThread = CPLCreateJoinableThread(thread_func2, nullptr);
     155           1 :     CPLSleep(0.2);
     156             : 
     157           1 :     GByte c = 0;
     158           1 :     CPL_IGNORE_RET_VAL(poDS->GetRasterBand(1)->RasterIO(
     159             :         GF_Read, 0, 0, 1, 1, &c, 1, 1, GDT_Byte, 0, 0, nullptr));
     160           1 :     printf("%d\n", c);
     161           1 :     ASSERT_EQ(c, 1);
     162           1 :     CPLJoinThread(hThread);
     163             : 
     164           1 :     CPLSetConfigOption("GDAL_RB_INTERNALIZE_SLEEP_AFTER_DETACH_BEFORE_WRITE",
     165             :                        nullptr);
     166           1 :     delete poDS;
     167           1 :     VSIUnlink("/vsimem/foo.tif");
     168           1 :     printf("End test2\n");
     169             : }
     170             : 
     171             : // ---------------------------------------------------------------------------
     172             : 
     173           4 : TEST(testblockcachelimits, test)
     174             : {
     175           1 :     CPLSetConfigOption("GDAL_DEBUG_BLOCK_CACHE", "ON");
     176           1 :     GDALGetCacheMax();
     177             : 
     178           1 :     GDALAllRegister();
     179             : 
     180           1 :     test1();
     181           1 :     test2();
     182             : 
     183           1 :     GDALDestroyDriverManager();
     184           1 : }
     185             : 
     186             : }  // namespace

Generated by: LCOV version 1.14