gitlab.com/CoiaPrant/sqlite3@v1.19.1/testdata/tcl/malloc5.test (about)

     1  # 2005 November 30
     2  #
     3  # The author disclaims copyright to this source code.  In place of
     4  # a legal notice, here is a blessing:
     5  #
     6  #    May you do good and not evil.
     7  #    May you find forgiveness for yourself and forgive others.
     8  #    May you share freely, never taking more than you give.
     9  #
    10  #***********************************************************************
    11  #
    12  # This file contains test cases focused on the two memory-management APIs, 
    13  # sqlite3_soft_heap_limit() and sqlite3_release_memory().
    14  #
    15  # Prior to version 3.6.2, calling sqlite3_release_memory() or exceeding
    16  # the configured soft heap limit could cause sqlite to upgrade database 
    17  # locks and flush dirty pages to the file system. As of 3.6.2, this is
    18  # no longer the case. In version 3.6.2, sqlite3_release_memory() only
    19  # reclaims clean pages. This test file has been updated accordingly.
    20  #
    21  # $Id: malloc5.test,v 1.22 2009/04/11 19:09:54 drh Exp $
    22  
    23  set testdir [file dirname $argv0]
    24  source $testdir/tester.tcl
    25  source $testdir/malloc_common.tcl
    26  db close
    27  
    28  # Only run these tests if memory debugging is turned on.
    29  #
    30  if {!$MEMDEBUG} {
    31     puts "Skipping malloc5 tests: not compiled with -DSQLITE_MEMDEBUG..."
    32     finish_test
    33     return
    34  }
    35  
    36  # Skip these tests if OMIT_MEMORY_MANAGEMENT was defined at compile time.
    37  ifcapable !memorymanage {
    38     finish_test
    39     return
    40  }
    41  
    42  # The sizes of memory allocations from system malloc() might vary,
    43  # depending on the memory allocator algorithms used.  The following
    44  # routine is designed to support answers that fall within a range
    45  # of values while also supplying easy-to-understand "expected" values
    46  # when errors occur.
    47  #
    48  proc value_in_range {target x args} {
    49    set v [lindex $args 0]
    50    if {$v!=""} {
    51      if {$v<$target*$x} {return $v}
    52      if {$v>$target/$x} {return $v}
    53    }
    54    return "number between [expr {int($target*$x)}] and [expr {int($target/$x)}]"
    55  }
    56  set mrange 0.98   ;#  plus or minus 2%
    57  
    58  test_set_config_pagecache 0 100
    59  
    60  sqlite3_soft_heap_limit 0
    61  sqlite3 db test.db
    62  # db eval {PRAGMA cache_size=1}
    63  
    64  do_test malloc5-1.1 {
    65    # Simplest possible test. Call sqlite3_release_memory when there is exactly
    66    # one unused page in a single pager cache. The page cannot be freed, as
    67    # it is dirty. So sqlite3_release_memory() returns 0.
    68    #
    69    execsql {
    70      PRAGMA auto_vacuum=OFF;
    71      BEGIN;
    72      CREATE TABLE abc(a, b, c);
    73    }
    74    sqlite3_release_memory
    75  } {0}
    76  
    77  do_test malloc5-1.2 {
    78    # Test that the transaction started in the above test is still active.
    79    # The lock on the database file should not have been upgraded (this was
    80    # not the case before version 3.6.2).
    81    #
    82    sqlite3 db2 test.db
    83    execsql {PRAGMA cache_size=2; SELECT * FROM sqlite_master } db2
    84  } {}
    85  do_test malloc5-1.3 {
    86    # Call [sqlite3_release_memory] when there is exactly one unused page 
    87    # in the cache belonging to db2.
    88    #
    89    set ::pgalloc [sqlite3_release_memory]
    90    value_in_range 1288 0.75
    91  } [value_in_range 1288 0.75]
    92  
    93  do_test malloc5-1.4 {
    94    # Commit the transaction and open a new one. Read 1 page into the cache.
    95    # Because the page is not dirty, it is eligible for collection even
    96    # before the transaction is concluded.
    97    #
    98    execsql {
    99      COMMIT;
   100      BEGIN;
   101      SELECT * FROM abc;
   102    }
   103    value_in_range $::pgalloc $::mrange [sqlite3_release_memory]
   104  } [value_in_range $::pgalloc $::mrange]
   105  
   106  do_test malloc5-1.5 {
   107    # Conclude the transaction opened in the previous [do_test] block. This
   108    # causes another page (page 1) to become eligible for recycling.
   109    #
   110    execsql { COMMIT }
   111    value_in_range $::pgalloc $::mrange [sqlite3_release_memory]
   112  } [value_in_range $::pgalloc $::mrange]
   113  
   114  do_test malloc5-1.6 {
   115    # Manipulate the cache so that it contains two unused pages. One requires 
   116    # a journal-sync to free, the other does not.
   117    db2 close
   118    execsql {
   119      BEGIN;
   120      CREATE TABLE def(d, e, f);
   121      SELECT * FROM abc;
   122    }
   123    value_in_range $::pgalloc $::mrange [sqlite3_release_memory 500]
   124  } [value_in_range $::pgalloc $::mrange]
   125  do_test malloc5-1.7 {
   126    # Database should not be locked this time. 
   127    sqlite3 db2 test.db
   128    catchsql { SELECT * FROM abc } db2
   129  } {0 {}}
   130  do_test malloc5-1.8 {
   131    # Try to release another block of memory. This will fail as the only
   132    # pages currently in the cache are dirty (page 3) or pinned (page 1).
   133    db2 close
   134    sqlite3_release_memory 500
   135  } 0
   136  do_test malloc5-1.8 {
   137    # Database is still not locked.
   138    #
   139    sqlite3 db2 test.db
   140    catchsql { SELECT * FROM abc } db2
   141  } {0 {}}
   142  do_test malloc5-1.9 {
   143    execsql {
   144      COMMIT;
   145    }
   146  } {}
   147  
   148  do_test malloc5-2.1 {
   149    # Put some data in tables abc and def. Both tables are still wholly 
   150    # contained within their root pages.
   151    execsql {
   152      INSERT INTO abc VALUES(1, 2, 3);
   153      INSERT INTO abc VALUES(4, 5, 6);
   154      INSERT INTO def VALUES(7, 8, 9);
   155      INSERT INTO def VALUES(10,11,12);
   156    }
   157  } {}
   158  do_test malloc5-2.2 {
   159    # Load the root-page for table def into the cache. Then query table abc. 
   160    # Halfway through the query call sqlite3_release_memory(). The goal of this
   161    # test is to make sure we don't free pages that are in use (specifically, 
   162    # the root of table abc).
   163    sqlite3_release_memory
   164    set nRelease 0
   165    execsql { 
   166      BEGIN;
   167      SELECT * FROM def;
   168    }
   169    set data [list]
   170    db eval {SELECT * FROM abc} {
   171      incr nRelease [sqlite3_release_memory]
   172      lappend data $a $b $c
   173    }
   174    execsql {
   175      COMMIT;
   176    }
   177    value_in_range $::pgalloc $::mrange $nRelease
   178  } [value_in_range $::pgalloc $::mrange]
   179  do_test malloc5-2.2.1 {
   180    set data
   181  } {1 2 3 4 5 6}
   182  
   183  do_test malloc5-3.1 {
   184    # Simple test to show that if two pagers are opened from within this
   185    # thread, memory is freed from both when sqlite3_release_memory() is
   186    # called.
   187    execsql {
   188      BEGIN;
   189      SELECT * FROM abc;
   190    }
   191    execsql {
   192      SELECT * FROM sqlite_master;
   193      BEGIN;
   194      SELECT * FROM def;
   195    } db2
   196    value_in_range [expr $::pgalloc*2] 0.99 [sqlite3_release_memory]
   197  } [value_in_range [expr $::pgalloc * 2] 0.99]
   198  do_test malloc5-3.2 {
   199    concat \
   200      [execsql {SELECT * FROM abc; COMMIT}] \
   201      [execsql {SELECT * FROM def; COMMIT} db2]
   202  } {1 2 3 4 5 6 7 8 9 10 11 12}
   203  
   204  db2 close
   205  puts "Highwater mark: [sqlite3_memory_highwater]"
   206  
   207  # The following two test cases each execute a transaction in which 
   208  # 10000 rows are inserted into table abc. The first test case is used
   209  # to ensure that more than 1MB of dynamic memory is used to perform
   210  # the transaction. 
   211  #
   212  # The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
   213  # and tests to see that this limit is not exceeded at any point during 
   214  # transaction execution.
   215  #
   216  # Before executing malloc5-4.* we save the value of the current soft heap 
   217  # limit in variable ::soft_limit. The original value is restored after 
   218  # running the tests.
   219  #
   220  set ::soft_limit [sqlite3_soft_heap_limit -1]
   221  execsql {PRAGMA cache_size=2000}
   222  
   223  # Test requires sqliteconfig.FbMemstat = 1 to measure highwater mark.
   224  # We are not built with that enabled, currently
   225  # -DSQLITE_DEFAULT_MEMSTATUS=0
   226  if {$::tcl_platform(platform)!="windows"} {
   227  
   228  do_test malloc5-4.1 {
   229    execsql {BEGIN;}
   230    execsql {DELETE FROM abc;}
   231    for {set i 0} {$i < 10000} {incr i} {
   232      execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
   233    }
   234    execsql {COMMIT;}
   235    db cache flush
   236    sqlite3_release_memory
   237    sqlite3_memory_highwater 1
   238    execsql {SELECT * FROM abc}
   239    set nMaxBytes [sqlite3_memory_highwater 1]
   240    puts -nonewline " (Highwater mark: $nMaxBytes) "
   241    expr $nMaxBytes > 1000000
   242  } {1}
   243  
   244  do_test malloc5-4.2 {
   245    db eval {PRAGMA cache_size=1}
   246    db cache flush
   247    sqlite3_release_memory
   248    sqlite3_soft_heap_limit 200000
   249    sqlite3_memory_highwater 1
   250    execsql {SELECT * FROM abc}
   251    set nMaxBytes [sqlite3_memory_highwater 1]
   252    puts -nonewline " (Highwater mark: $nMaxBytes) "
   253    expr $nMaxBytes <= 210000
   254  } {1}
   255  
   256  do_test malloc5-4.3 {
   257    # Check that the content of table abc is at least roughly as expected.
   258    execsql {
   259      SELECT count(*), sum(a), sum(b) FROM abc;
   260    }
   261  } [list 10000 [expr int(10000.0 * 4999.5)] [expr int(10000.0 * 4999.5)]]
   262  
   263  }
   264  
   265  # Restore the soft heap limit.
   266  sqlite3_soft_heap_limit $::soft_limit
   267  
   268  # Test that there are no problems calling sqlite3_release_memory when
   269  # there are open in-memory databases.
   270  #
   271  # At one point these tests would cause a seg-fault.
   272  #
   273  do_test malloc5-5.1 {
   274    db close
   275    sqlite3 db :memory:
   276    execsql {
   277      BEGIN;
   278      CREATE TABLE abc(a, b, c);
   279      INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL);
   280      INSERT INTO abc SELECT * FROM abc;
   281      INSERT INTO abc SELECT * FROM abc;
   282      INSERT INTO abc SELECT * FROM abc;
   283      INSERT INTO abc SELECT * FROM abc;
   284      INSERT INTO abc SELECT * FROM abc;
   285      INSERT INTO abc SELECT * FROM abc;
   286      INSERT INTO abc SELECT * FROM abc;
   287    }
   288    sqlite3_release_memory
   289  } 0
   290  do_test malloc5-5.2 {
   291    sqlite3_soft_heap_limit 5000
   292    execsql {
   293      COMMIT;
   294      PRAGMA temp_store = memory;
   295      SELECT * FROM abc ORDER BY a;
   296    }
   297    expr 1
   298  } {1}
   299  sqlite3_soft_heap_limit $::soft_limit
   300  
   301  #-------------------------------------------------------------------------
   302  # The following test cases (malloc5-6.*) test the new global LRU list
   303  # used to determine the pages to recycle when sqlite3_release_memory is
   304  # called and there is more than one pager open.
   305  #
   306  proc nPage {db} {
   307    set bt [btree_from_db $db]
   308    array set stats [btree_pager_stats $bt]
   309    set stats(page)
   310  }
   311  db close
   312  forcedelete test.db test.db-journal test2.db test2.db-journal
   313  
   314  # This block of test-cases (malloc5-6.1.*) prepares two database files
   315  # for the subsequent tests.
   316  do_test malloc5-6.1.1 {
   317    sqlite3 db test.db
   318    execsql {
   319      PRAGMA page_size=1024;
   320      PRAGMA default_cache_size=2;
   321    }
   322    execsql {
   323      PRAGMA temp_store = memory;
   324      BEGIN;
   325      CREATE TABLE abc(a PRIMARY KEY, b, c);
   326      INSERT INTO abc VALUES(randstr(50,50), randstr(75,75), randstr(100,100));
   327      INSERT INTO abc 
   328          SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
   329      INSERT INTO abc 
   330          SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
   331      INSERT INTO abc 
   332          SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
   333      INSERT INTO abc 
   334          SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
   335      INSERT INTO abc 
   336          SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
   337      INSERT INTO abc 
   338          SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
   339      COMMIT;
   340    } 
   341    forcecopy test.db test2.db
   342    sqlite3 db2 test2.db
   343    db2 eval {PRAGMA cache_size=2}
   344    list \
   345      [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20]
   346  } {1 1}
   347  do_test malloc5-6.1.2 {
   348    list [execsql {PRAGMA cache_size}] [execsql {PRAGMA cache_size} db2]
   349  } {2 2}
   350  
   351  do_test malloc5-6.2.1 {
   352    execsql {SELECT * FROM abc} db2
   353    execsql {SELECT * FROM abc} db
   354    expr [nPage db] + [nPage db2]
   355  } {4}
   356  
   357  
   358  # Our min-useable malloc block-size appears to be 2k (actual) 
   359  # Because this test attempts to measure actual memory freed 
   360  # causing 2 blocks to be freed will free 4K, failing the tests
   361  if {$::tcl_platform(platform)!="windows"} {
   362  
   363  do_test malloc5-6.2.2 {
   364    # If we now try to reclaim some memory, it should come from the db2 cache.
   365    sqlite3_release_memory 3000
   366    expr [nPage db] + [nPage db2]
   367  } {1}
   368  do_test malloc5-6.2.3 {
   369    # Access the db2 cache again, so that all the db2 pages have been used
   370    # more recently than all the db pages. Then try to reclaim 3000 bytes.
   371    # This time, 3 pages should be pulled from the db cache.
   372    execsql { SELECT * FROM abc } db2
   373    sqlite3_release_memory 3000
   374    expr [nPage db] + [nPage db2]
   375  } {0}
   376  
   377  }
   378  
   379  do_test malloc5-6.3.1 {
   380    # Now open a transaction and update 2 pages in the db2 cache. Then
   381    # do a SELECT on the db cache so that all the db pages are more recently
   382    # used than the db2 pages. When we try to free memory, SQLite should
   383    # free the non-dirty db2 pages, then the db pages, then finally use
   384    # sync() to free up the dirty db2 pages. The only page that cannot be
   385    # freed is page1 of db2. Because there is an open transaction, the
   386    # btree layer holds a reference to page 1 in the db2 cache.
   387    #
   388    # UPDATE: No longer. As release_memory() does not cause a sync()
   389    execsql {
   390      BEGIN;
   391      UPDATE abc SET c = randstr(100,100) 
   392      WHERE rowid = 1 OR rowid = (SELECT max(rowid) FROM abc);
   393    } db2
   394    execsql { SELECT * FROM abc } db
   395    expr [nPage db] + [nPage db2]
   396  } {4}
   397  do_test malloc5-6.3.2 {
   398    # Try to release 7700 bytes. This should release all the 
   399    # non-dirty pages held by db2.
   400    sqlite3_release_memory [expr 7*1132]
   401    list [nPage db] [nPage db2]
   402  } {0 3}
   403  do_test malloc5-6.3.3 {
   404    # Try to release another 1000 bytes. This should come fromt the db
   405    # cache, since all three pages held by db2 are either in-use or diry.
   406    sqlite3_release_memory 1000
   407    list [nPage db] [nPage db2]
   408  } {0 3}
   409  do_test malloc5-6.3.4 {
   410    # Now release 9900 more (about 9 pages worth). This should expunge
   411    # the rest of the db cache. But the db2 cache remains intact, because
   412    # SQLite tries to avoid calling sync().
   413    if {$::tcl_platform(wordSize)==8} {
   414      sqlite3_release_memory 10500
   415    } else {
   416      sqlite3_release_memory 9900
   417    }
   418    list [nPage db] [nPage db2]
   419  } {0 3}
   420  do_test malloc5-6.3.5 {
   421    # But if we are really insistent, SQLite will consent to call sync()
   422    # if there is no other option. UPDATE: As of 3.6.2, SQLite will not
   423    # call sync() in this scenario. So no further memory can be reclaimed.
   424    sqlite3_release_memory 1000
   425    list [nPage db] [nPage db2]
   426  } {0 3}
   427  do_test malloc5-6.3.6 {
   428    # The referenced page (page 1 of the db2 cache) will not be freed no
   429    # matter how much memory we ask for:
   430    sqlite3_release_memory 31459
   431    list [nPage db] [nPage db2]
   432  } {0 3}
   433  
   434  db2 close
   435  
   436  sqlite3_soft_heap_limit $::soft_limit
   437  test_restore_config_pagecache
   438  finish_test
   439  catch {db close}