Re: [PATCH 05/10] Invalidate or shrink dcache when setting is changed.

Yao Qi
2013-11-18T14:12:06Z

On 11/18/2013 04:34 AM, Doug Evans wrote:
> It feels simpler if this were:
>
> if (target_dcache_init_p ())
> dcache_check_resize (target_dcache);
> return target_dcache;
>
> There may be a better name than dcache_check_resize, one may want to
> call it for situations other than resizings, but here target-dcache.c
> is using 3 API routines from dcache.c to achieve what is essentially
> one operation: check if the cache needs to be reconfigured and update
> if so.

How about "dcache_update_config"?

>
>> >@@ -54,7 +62,14 @@ target_dcache_get (void)
>> > DCACHE *
>> > target_dcache_get_or_init (void)
>> > {
>> >- if (!target_dcache_init_p ())
>> >+ if (target_dcache_init_p ())
>> >+ {
>> >+ if (dcache_invalidate_p (target_dcache))
>> >+ dcache_invalidate (target_dcache);
>> >+
>> >+ dcache_shrink (target_dcache);
>> >+ }
>> >+ else
>> > target_dcache = dcache_init ();
>> >
>> > return target_dcache;
> I think it would be better to remove the duplication and have:
>
> if (target_dcache_init_p ())
> return target_dcache_get ();

Fixed.

--
Yao (é½å°§)

gdb:

2013-11-18 Yao Qi <yao@codesourcery.com>

* dcache.c (dcache_evict): New function.
(dcache_shrink): New function.
(dcache_update_config): New function.
(dcache_alloc): Call dcache_evict.
(set_dcache_size): Remove call target_dcache_invalidate.
(set_dcache_line_size): Likewise.
* dcache.h (dcache_update_config): Declare.
* target-dcache.c (target_dcache_get): Invalidate and shrink
cache if necessary.
(target_dcache_get_or_init): Likewise.

gdb/doc:

2013-11-18 Yao Qi <yao@codesourcery.com>

* gdb.texinfo (Caching Target Data): Update document of
commands 'set dcache size' and 'set dcache line-size'.
---
gdb/dcache.c | 53 ++++++++++++++++++++++++++++++++++++++++++--------
gdb/dcache.h | 2 +
gdb/doc/gdb.texinfo | 7 ++++-
gdb/target-dcache.c | 7 +++++-
4 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/gdb/dcache.c b/gdb/dcache.c
index 5b32629..4474be4 100644
--- a/gdb/dcache.c
+++ b/gdb/dcache.c
@@ -243,6 +243,49 @@ invalidate_block (struct dcache_block *block, void *param)
append_block (&dcache->freelist, block);
}

+/* Evict the cache line decided by the eviction algorithm. Return the
+ evicted cache line. */
+
+static struct dcache_block *
+dcache_evict (DCACHE *dcache)
+{
+ /* Evict the least recently allocated line. */
+ struct dcache_block *db = dcache->oldest;
+
+ remove_block (&dcache->oldest, db);
+ splay_tree_remove (dcache->tree, (splay_tree_key) db->addr);
+
+ return db;
+}
+
+/* Shrink DCACHE if it is over-sized. */
+
+static void
+dcache_shrink (DCACHE *dcache)
+{
+ while (dcache->size > dcache_size)
+ {
+ struct dcache_block *db = dcache_evict (dcache);
+
+ free_block (db, NULL);
+ dcache->size--;
+ }
+}
+
+/* Update the configuration, such as line-size, of DCACHE if
+ necessary. */
+
+void
+dcache_update_config (DCACHE *dcache)
+{
+ /* DCACHE must be invalidated if its line-size is different from the
+ global setting. */
+ if (dcache->line_size != dcache_line_size)
+ dcache_invalidate (dcache);
+
+ dcache_shrink (dcache);
+}
+
/* Free all the data cache blocks, thus discarding all cached data. */

void
@@ -359,13 +402,7 @@ dcache_alloc (DCACHE *dcache, CORE_ADDR addr)
struct dcache_block *db;

if (dcache->size >= dcache_size)
- {
- /* Evict the least recently allocated line. */
- db = dcache->oldest;
- remove_block (&dcache->oldest, db);
-
- splay_tree_remove (dcache->tree, (splay_tree_key) db->addr);
- }
+ db = dcache_evict (dcache);
else
{
db = dcache->freelist;
@@ -669,7 +706,6 @@ set_dcache_size (char *args, int from_tty,
dcache_size = DCACHE_DEFAULT_SIZE;
error (_("Dcache size must be greater than 0."));
}
- target_dcache_invalidate ();
}

static void
@@ -683,7 +719,6 @@ set_dcache_line_size (char *args, int from_tty,
dcache_line_size = DCACHE_DEFAULT_LINE_SIZE;
error (_("Invalid dcache line size: %u (must be power of 2)."), d);
}
- target_dcache_invalidate ();
}

static void
diff --git a/gdb/dcache.h b/gdb/dcache.h
index 720a887..d2fcd6a 100644
--- a/gdb/dcache.h
+++ b/gdb/dcache.h
@@ -40,4 +40,6 @@ int dcache_xfer_memory (struct target_ops *ops, DCACHE *cache, CORE_ADDR mem,
void dcache_update (DCACHE *dcache, CORE_ADDR memaddr, gdb_byte *myaddr,
int len);

+void dcache_update_config (DCACHE *dcache);
+
#endif /* DCACHE_H */
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 8377ca8..16ae0c8 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -10878,13 +10878,16 @@ printed in hex.
@item set dcache size @var{size}
@cindex dcache size
@kindex set dcache size
-Set maximum number of entries in dcache (dcache depth above).
+Set maximum number of entries in dcache (dcache depth above). When
+the dcache is being used, if its size is greater than this setting, it
+is shrunk.

@item set dcache line-size @var{line-size}
@cindex dcache line-size
@kindex set dcache line-size
Set number of bytes each dcache entry caches (dcache width above).
-Must be a power of 2.
+Must be a power of 2. When the dcache is being used, if its width is
+different from this setting, it is invalidated.

@item show dcache size
@kindex show dcache size
diff --git a/gdb/target-dcache.c b/gdb/target-dcache.c
index bf04679..6bd576d 100644
--- a/gdb/target-dcache.c
+++ b/gdb/target-dcache.c
@@ -45,6 +45,9 @@ target_dcache_invalidate (void)
DCACHE *
target_dcache_get (void)
{
+ if (target_dcache_init_p ())
+ dcache_update_config (target_dcache);
+
return target_dcache;
}

@@ -54,7 +57,9 @@ target_dcache_get (void)
DCACHE *
target_dcache_get_or_init (void)
{
- if (!target_dcache_init_p ())
+ if (target_dcache_init_p ())
+ return target_dcache_get ();
+ else
target_dcache = dcache_init ();

return target_dcache;
--
1.7.7.6