Re: [Mesa-dev] [PATCH] r600g: add cs memory usage accounting and limit it

2013-01-31 Thread Jerome Glisse
On Wed, Jan 30, 2013 at 10:35 PM, Marek Olšák mar...@gmail.com wrote:
 On Wed, Jan 30, 2013 at 6:14 PM,  j.gli...@gmail.com wrote:
 From: Jerome Glisse jgli...@redhat.com

 We are now seing cs that can go over the vram+gtt size to avoid
 failing flush early cs that goes over 70% (gtt+vram) usage. 70%
 is use to allow some fragmentation.

 Signed-off-by: Jerome Glisse jgli...@redhat.com
 ---
  src/gallium/drivers/r600/evergreen_state.c|  4 
  src/gallium/drivers/r600/r600.h   |  1 +
  src/gallium/drivers/r600/r600_buffer.c|  1 +
  src/gallium/drivers/r600/r600_hw_context.c| 12 
  src/gallium/drivers/r600/r600_pipe.c  |  3 +++
  src/gallium/drivers/r600/r600_pipe.h  | 21 +
  src/gallium/drivers/r600/r600_state.c |  3 +++
  src/gallium/drivers/r600/r600_state_common.c  | 17 -
  src/gallium/winsys/radeon/drm/radeon_drm_cs.c | 11 +++
  src/gallium/winsys/radeon/drm/radeon_winsys.h | 10 ++
  10 files changed, 82 insertions(+), 1 deletion(-)

 diff --git a/src/gallium/drivers/r600/evergreen_state.c 
 b/src/gallium/drivers/r600/evergreen_state.c
 index be1c427..84f8dce 100644
 --- a/src/gallium/drivers/r600/evergreen_state.c
 +++ b/src/gallium/drivers/r600/evergreen_state.c
 @@ -1668,6 +1668,8 @@ static void evergreen_set_framebuffer_state(struct 
 pipe_context *ctx,
 surf = (struct r600_surface*)state-cbufs[i];
 rtex = (struct r600_texture*)surf-base.texture;

 +   r600_context_add_resource_size(ctx, 
 state-cbufs[i]-texture);
 +
 if (!surf-color_initialized) {
 evergreen_init_color_surface(rctx, surf);
 }
 @@ -1699,6 +1701,8 @@ static void evergreen_set_framebuffer_state(struct 
 pipe_context *ctx,
 if (state-zsbuf) {
 surf = (struct r600_surface*)state-zsbuf;

 +   r600_context_add_resource_size(ctx, state-zsbuf-texture);
 +
 if (!surf-depth_initialized) {
 evergreen_init_depth_surface(rctx, surf);
 }
 diff --git a/src/gallium/drivers/r600/r600.h 
 b/src/gallium/drivers/r600/r600.h
 index a383c90..b9f7d3d 100644
 --- a/src/gallium/drivers/r600/r600.h
 +++ b/src/gallium/drivers/r600/r600.h
 @@ -50,6 +50,7 @@ struct r600_resource {

 /* Resource state. */
 unsigneddomains;
 +   uint64_tsize;

 Don't add this. Use r600_resource::buf::size instead, which is already
 initialized.


  };

  #define R600_BLOCK_MAX_BO  32
 diff --git a/src/gallium/drivers/r600/r600_buffer.c 
 b/src/gallium/drivers/r600/r600_buffer.c
 index 6df0d91..92f549a 100644
 --- a/src/gallium/drivers/r600/r600_buffer.c
 +++ b/src/gallium/drivers/r600/r600_buffer.c
 @@ -250,6 +250,7 @@ bool r600_init_resource(struct r600_screen *rscreen,
 break;
 }

 +   res-size = size;
 res-buf = rscreen-ws-buffer_create(rscreen-ws, size, alignment,
use_reusable_pool,
initial_domain);
 diff --git a/src/gallium/drivers/r600/r600_hw_context.c 
 b/src/gallium/drivers/r600/r600_hw_context.c
 index ebafd97..44d3b4d 100644
 --- a/src/gallium/drivers/r600/r600_hw_context.c
 +++ b/src/gallium/drivers/r600/r600_hw_context.c
 @@ -359,6 +359,16 @@ out_err:
  void r600_need_cs_space(struct r600_context *ctx, unsigned num_dw,
 boolean count_draw_in)
  {
 +   if (!ctx-ws-cs_memory_below_limit(ctx-rings.gfx.cs, ctx-vram, 
 ctx-gtt)) {
 +   ctx-gtt = 0;
 +   ctx-vram = 0;
 +   ctx-rings.gfx.flush(ctx, RADEON_FLUSH_ASYNC);
 +   return;
 +   }
 +   /* all will be accounted once relocation are emited */
 +   ctx-gtt = 0;
 +   ctx-vram = 0;
 +
 /* The number of dwords we already used in the CS so far. */
 num_dw += ctx-rings.gfx.cs-cdw;

 @@ -784,6 +794,8 @@ void r600_begin_new_cs(struct r600_context *ctx)

 ctx-pm4_dirty_cdwords = 0;
 ctx-flags = 0;
 +   ctx-gtt = 0;
 +   ctx-vram = 0;

 /* Begin a new CS. */
 r600_emit_command_buffer(ctx-rings.gfx.cs, ctx-start_cs_cmd);
 diff --git a/src/gallium/drivers/r600/r600_pipe.c 
 b/src/gallium/drivers/r600/r600_pipe.c
 index a59578d..cb50cfe 100644
 --- a/src/gallium/drivers/r600/r600_pipe.c
 +++ b/src/gallium/drivers/r600/r600_pipe.c
 @@ -333,6 +333,9 @@ static struct pipe_context *r600_create_context(struct 
 pipe_screen *screen, void
 rctx-chip_class = rscreen-chip_class;
 rctx-keep_tiling_flags = rscreen-info.drm_minor = 12;

 +   rctx-gtt = 0;
 +   rctx-vram = 0;

 There is no reason to initialize anything to 0 in context_create. The
 whole context structure is calloc'd.


 +
 LIST_INITHEAD(rctx-active_nontimer_queries);
 

[Mesa-dev] [PATCH] r600g: add cs memory usage accounting and limit it v2

2013-01-31 Thread j . glisse
From: Jerome Glisse jgli...@redhat.com

We are now seing cs that can go over the vram+gtt size to avoid
failing flush early cs that goes over 70% (gtt+vram) usage. 70%
is use to allow some fragmentation.

The idea is to compute a gross estimate of memory requirement of
each draw call. After each draw call, memory will be precisely
accounted. So the uncertainty is only on the current draw call.
In practice this gave very good estimate (+/- 10% of the target
memory limit).

v2: Remove left over from testing version, remove useless NULL
checking. Improve commit message.

Signed-off-by: Jerome Glisse jgli...@redhat.com
---
 src/gallium/drivers/r600/evergreen_state.c|  4 
 src/gallium/drivers/r600/r600_hw_context.c| 12 
 src/gallium/drivers/r600/r600_pipe.h  | 21 +
 src/gallium/drivers/r600/r600_state.c |  3 +++
 src/gallium/drivers/r600/r600_state_common.c  | 13 -
 src/gallium/winsys/radeon/drm/radeon_drm_cs.c | 11 +++
 src/gallium/winsys/radeon/drm/radeon_winsys.h | 10 ++
 7 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/src/gallium/drivers/r600/evergreen_state.c 
b/src/gallium/drivers/r600/evergreen_state.c
index 0a3861f..5dd8b13 100644
--- a/src/gallium/drivers/r600/evergreen_state.c
+++ b/src/gallium/drivers/r600/evergreen_state.c
@@ -1668,6 +1668,8 @@ static void evergreen_set_framebuffer_state(struct 
pipe_context *ctx,
surf = (struct r600_surface*)state-cbufs[i];
rtex = (struct r600_texture*)surf-base.texture;
 
+   r600_context_add_resource_size(ctx, state-cbufs[i]-texture);
+
if (!surf-color_initialized) {
evergreen_init_color_surface(rctx, surf);
}
@@ -1699,6 +1701,8 @@ static void evergreen_set_framebuffer_state(struct 
pipe_context *ctx,
if (state-zsbuf) {
surf = (struct r600_surface*)state-zsbuf;
 
+   r600_context_add_resource_size(ctx, state-zsbuf-texture);
+
if (!surf-depth_initialized) {
evergreen_init_depth_surface(rctx, surf);
}
diff --git a/src/gallium/drivers/r600/r600_hw_context.c 
b/src/gallium/drivers/r600/r600_hw_context.c
index 23f488a..a89f230 100644
--- a/src/gallium/drivers/r600/r600_hw_context.c
+++ b/src/gallium/drivers/r600/r600_hw_context.c
@@ -359,6 +359,16 @@ out_err:
 void r600_need_cs_space(struct r600_context *ctx, unsigned num_dw,
boolean count_draw_in)
 {
+   if (!ctx-ws-cs_memory_below_limit(ctx-rings.gfx.cs, ctx-vram, 
ctx-gtt)) {
+   ctx-gtt = 0;
+   ctx-vram = 0;
+   ctx-rings.gfx.flush(ctx, RADEON_FLUSH_ASYNC);
+   return;
+   }
+   /* all will be accounted once relocation are emited */
+   ctx-gtt = 0;
+   ctx-vram = 0;
+
/* The number of dwords we already used in the CS so far. */
num_dw += ctx-rings.gfx.cs-cdw;
 
@@ -784,6 +794,8 @@ void r600_begin_new_cs(struct r600_context *ctx)
 
ctx-pm4_dirty_cdwords = 0;
ctx-flags = 0;
+   ctx-gtt = 0;
+   ctx-vram = 0;
 
/* Begin a new CS. */
r600_emit_command_buffer(ctx-rings.gfx.cs, ctx-start_cs_cmd);
diff --git a/src/gallium/drivers/r600/r600_pipe.h 
b/src/gallium/drivers/r600/r600_pipe.h
index 3ff42d3..42b4e7c 100644
--- a/src/gallium/drivers/r600/r600_pipe.h
+++ b/src/gallium/drivers/r600/r600_pipe.h
@@ -447,6 +447,10 @@ struct r600_context {
unsignedbackend_mask;
unsignedmax_db; /* for OQ */
 
+   /* current unaccounted memory usage */
+   uint64_tvram;
+   uint64_tgtt;
+
/* Miscellaneous state objects. */
void*custom_dsa_flush;
void*custom_blend_resolve;
@@ -998,4 +1002,21 @@ static INLINE unsigned u_max_layer(struct pipe_resource 
*r, unsigned level)
}
 }
 
+static INLINE void r600_context_add_resource_size(struct pipe_context *ctx, 
struct pipe_resource *r)
+{
+   struct r600_context *rctx = (struct r600_context *)ctx;
+   struct r600_resource *rr = (struct r600_resource *)r;
+
+   if (r == NULL) {
+   return;
+   }
+
+   if (rr-domains  RADEON_DOMAIN_GTT) {
+   rctx-gtt += rr-buf-size;
+   }
+   if (rr-domains  RADEON_DOMAIN_VRAM) {
+   rctx-vram += rr-buf-size;
+   }
+}
+
 #endif
diff --git a/src/gallium/drivers/r600/r600_state.c 
b/src/gallium/drivers/r600/r600_state.c
index c0bc2a5..44cd00e 100644
--- a/src/gallium/drivers/r600/r600_state.c
+++ b/src/gallium/drivers/r600/r600_state.c
@@ -1544,6 +1544,7 @@ static void r600_set_framebuffer_state(struct 
pipe_context *ctx,
 
surf = (struct r600_surface*)state-cbufs[i];
rtex = (struct r600_texture*)surf-base.texture;
+   

Re: [Mesa-dev] [PATCH] r600g: add cs memory usage accounting and limit it

2013-01-31 Thread Marek Olšák
On Thu, Jan 31, 2013 at 4:29 PM, Jerome Glisse j.gli...@gmail.com wrote:
 On Wed, Jan 30, 2013 at 10:35 PM, Marek Olšák mar...@gmail.com wrote:
 On Wed, Jan 30, 2013 at 6:14 PM,  j.gli...@gmail.com wrote:
 From: Jerome Glisse jgli...@redhat.com

 We are now seing cs that can go over the vram+gtt size to avoid
 failing flush early cs that goes over 70% (gtt+vram) usage. 70%
 is use to allow some fragmentation.

 Signed-off-by: Jerome Glisse jgli...@redhat.com
 ---
  src/gallium/drivers/r600/evergreen_state.c|  4 
  src/gallium/drivers/r600/r600.h   |  1 +
  src/gallium/drivers/r600/r600_buffer.c|  1 +
  src/gallium/drivers/r600/r600_hw_context.c| 12 
  src/gallium/drivers/r600/r600_pipe.c  |  3 +++
  src/gallium/drivers/r600/r600_pipe.h  | 21 +
  src/gallium/drivers/r600/r600_state.c |  3 +++
  src/gallium/drivers/r600/r600_state_common.c  | 17 -
  src/gallium/winsys/radeon/drm/radeon_drm_cs.c | 11 +++
  src/gallium/winsys/radeon/drm/radeon_winsys.h | 10 ++
  10 files changed, 82 insertions(+), 1 deletion(-)

 diff --git a/src/gallium/drivers/r600/evergreen_state.c 
 b/src/gallium/drivers/r600/evergreen_state.c
 index be1c427..84f8dce 100644
 --- a/src/gallium/drivers/r600/evergreen_state.c
 +++ b/src/gallium/drivers/r600/evergreen_state.c
 @@ -1668,6 +1668,8 @@ static void evergreen_set_framebuffer_state(struct 
 pipe_context *ctx,
 surf = (struct r600_surface*)state-cbufs[i];
 rtex = (struct r600_texture*)surf-base.texture;

 +   r600_context_add_resource_size(ctx, 
 state-cbufs[i]-texture);
 +
 if (!surf-color_initialized) {
 evergreen_init_color_surface(rctx, surf);
 }
 @@ -1699,6 +1701,8 @@ static void evergreen_set_framebuffer_state(struct 
 pipe_context *ctx,
 if (state-zsbuf) {
 surf = (struct r600_surface*)state-zsbuf;

 +   r600_context_add_resource_size(ctx, state-zsbuf-texture);
 +
 if (!surf-depth_initialized) {
 evergreen_init_depth_surface(rctx, surf);
 }
 diff --git a/src/gallium/drivers/r600/r600.h 
 b/src/gallium/drivers/r600/r600.h
 index a383c90..b9f7d3d 100644
 --- a/src/gallium/drivers/r600/r600.h
 +++ b/src/gallium/drivers/r600/r600.h
 @@ -50,6 +50,7 @@ struct r600_resource {

 /* Resource state. */
 unsigneddomains;
 +   uint64_tsize;

 Don't add this. Use r600_resource::buf::size instead, which is already
 initialized.


  };

  #define R600_BLOCK_MAX_BO  32
 diff --git a/src/gallium/drivers/r600/r600_buffer.c 
 b/src/gallium/drivers/r600/r600_buffer.c
 index 6df0d91..92f549a 100644
 --- a/src/gallium/drivers/r600/r600_buffer.c
 +++ b/src/gallium/drivers/r600/r600_buffer.c
 @@ -250,6 +250,7 @@ bool r600_init_resource(struct r600_screen *rscreen,
 break;
 }

 +   res-size = size;
 res-buf = rscreen-ws-buffer_create(rscreen-ws, size, alignment,
use_reusable_pool,
initial_domain);
 diff --git a/src/gallium/drivers/r600/r600_hw_context.c 
 b/src/gallium/drivers/r600/r600_hw_context.c
 index ebafd97..44d3b4d 100644
 --- a/src/gallium/drivers/r600/r600_hw_context.c
 +++ b/src/gallium/drivers/r600/r600_hw_context.c
 @@ -359,6 +359,16 @@ out_err:
  void r600_need_cs_space(struct r600_context *ctx, unsigned num_dw,
 boolean count_draw_in)
  {
 +   if (!ctx-ws-cs_memory_below_limit(ctx-rings.gfx.cs, ctx-vram, 
 ctx-gtt)) {
 +   ctx-gtt = 0;
 +   ctx-vram = 0;
 +   ctx-rings.gfx.flush(ctx, RADEON_FLUSH_ASYNC);
 +   return;
 +   }
 +   /* all will be accounted once relocation are emited */
 +   ctx-gtt = 0;
 +   ctx-vram = 0;
 +
 /* The number of dwords we already used in the CS so far. */
 num_dw += ctx-rings.gfx.cs-cdw;

 @@ -784,6 +794,8 @@ void r600_begin_new_cs(struct r600_context *ctx)

 ctx-pm4_dirty_cdwords = 0;
 ctx-flags = 0;
 +   ctx-gtt = 0;
 +   ctx-vram = 0;

 /* Begin a new CS. */
 r600_emit_command_buffer(ctx-rings.gfx.cs, ctx-start_cs_cmd);
 diff --git a/src/gallium/drivers/r600/r600_pipe.c 
 b/src/gallium/drivers/r600/r600_pipe.c
 index a59578d..cb50cfe 100644
 --- a/src/gallium/drivers/r600/r600_pipe.c
 +++ b/src/gallium/drivers/r600/r600_pipe.c
 @@ -333,6 +333,9 @@ static struct pipe_context *r600_create_context(struct 
 pipe_screen *screen, void
 rctx-chip_class = rscreen-chip_class;
 rctx-keep_tiling_flags = rscreen-info.drm_minor = 12;

 +   rctx-gtt = 0;
 +   rctx-vram = 0;

 There is no reason to initialize anything to 0 in context_create. The
 whole context structure is calloc'd.


 +
 

Re: [Mesa-dev] [PATCH] r600g: add cs memory usage accounting and limit it v2

2013-01-31 Thread Marek Olšák
Reviewed-by: Marek Olšák mar...@gmail.com

Marek

On Thu, Jan 31, 2013 at 4:38 PM,  j.gli...@gmail.com wrote:
 From: Jerome Glisse jgli...@redhat.com

 We are now seing cs that can go over the vram+gtt size to avoid
 failing flush early cs that goes over 70% (gtt+vram) usage. 70%
 is use to allow some fragmentation.

 The idea is to compute a gross estimate of memory requirement of
 each draw call. After each draw call, memory will be precisely
 accounted. So the uncertainty is only on the current draw call.
 In practice this gave very good estimate (+/- 10% of the target
 memory limit).

 v2: Remove left over from testing version, remove useless NULL
 checking. Improve commit message.

 Signed-off-by: Jerome Glisse jgli...@redhat.com
 ---
  src/gallium/drivers/r600/evergreen_state.c|  4 
  src/gallium/drivers/r600/r600_hw_context.c| 12 
  src/gallium/drivers/r600/r600_pipe.h  | 21 +
  src/gallium/drivers/r600/r600_state.c |  3 +++
  src/gallium/drivers/r600/r600_state_common.c  | 13 -
  src/gallium/winsys/radeon/drm/radeon_drm_cs.c | 11 +++
  src/gallium/winsys/radeon/drm/radeon_winsys.h | 10 ++
  7 files changed, 73 insertions(+), 1 deletion(-)

 diff --git a/src/gallium/drivers/r600/evergreen_state.c 
 b/src/gallium/drivers/r600/evergreen_state.c
 index 0a3861f..5dd8b13 100644
 --- a/src/gallium/drivers/r600/evergreen_state.c
 +++ b/src/gallium/drivers/r600/evergreen_state.c
 @@ -1668,6 +1668,8 @@ static void evergreen_set_framebuffer_state(struct 
 pipe_context *ctx,
 surf = (struct r600_surface*)state-cbufs[i];
 rtex = (struct r600_texture*)surf-base.texture;

 +   r600_context_add_resource_size(ctx, state-cbufs[i]-texture);
 +
 if (!surf-color_initialized) {
 evergreen_init_color_surface(rctx, surf);
 }
 @@ -1699,6 +1701,8 @@ static void evergreen_set_framebuffer_state(struct 
 pipe_context *ctx,
 if (state-zsbuf) {
 surf = (struct r600_surface*)state-zsbuf;

 +   r600_context_add_resource_size(ctx, state-zsbuf-texture);
 +
 if (!surf-depth_initialized) {
 evergreen_init_depth_surface(rctx, surf);
 }
 diff --git a/src/gallium/drivers/r600/r600_hw_context.c 
 b/src/gallium/drivers/r600/r600_hw_context.c
 index 23f488a..a89f230 100644
 --- a/src/gallium/drivers/r600/r600_hw_context.c
 +++ b/src/gallium/drivers/r600/r600_hw_context.c
 @@ -359,6 +359,16 @@ out_err:
  void r600_need_cs_space(struct r600_context *ctx, unsigned num_dw,
 boolean count_draw_in)
  {
 +   if (!ctx-ws-cs_memory_below_limit(ctx-rings.gfx.cs, ctx-vram, 
 ctx-gtt)) {
 +   ctx-gtt = 0;
 +   ctx-vram = 0;
 +   ctx-rings.gfx.flush(ctx, RADEON_FLUSH_ASYNC);
 +   return;
 +   }
 +   /* all will be accounted once relocation are emited */
 +   ctx-gtt = 0;
 +   ctx-vram = 0;
 +
 /* The number of dwords we already used in the CS so far. */
 num_dw += ctx-rings.gfx.cs-cdw;

 @@ -784,6 +794,8 @@ void r600_begin_new_cs(struct r600_context *ctx)

 ctx-pm4_dirty_cdwords = 0;
 ctx-flags = 0;
 +   ctx-gtt = 0;
 +   ctx-vram = 0;

 /* Begin a new CS. */
 r600_emit_command_buffer(ctx-rings.gfx.cs, ctx-start_cs_cmd);
 diff --git a/src/gallium/drivers/r600/r600_pipe.h 
 b/src/gallium/drivers/r600/r600_pipe.h
 index 3ff42d3..42b4e7c 100644
 --- a/src/gallium/drivers/r600/r600_pipe.h
 +++ b/src/gallium/drivers/r600/r600_pipe.h
 @@ -447,6 +447,10 @@ struct r600_context {
 unsignedbackend_mask;
 unsignedmax_db; /* for OQ */

 +   /* current unaccounted memory usage */
 +   uint64_tvram;
 +   uint64_tgtt;
 +
 /* Miscellaneous state objects. */
 void*custom_dsa_flush;
 void*custom_blend_resolve;
 @@ -998,4 +1002,21 @@ static INLINE unsigned u_max_layer(struct pipe_resource 
 *r, unsigned level)
 }
  }

 +static INLINE void r600_context_add_resource_size(struct pipe_context *ctx, 
 struct pipe_resource *r)
 +{
 +   struct r600_context *rctx = (struct r600_context *)ctx;
 +   struct r600_resource *rr = (struct r600_resource *)r;
 +
 +   if (r == NULL) {
 +   return;
 +   }
 +
 +   if (rr-domains  RADEON_DOMAIN_GTT) {
 +   rctx-gtt += rr-buf-size;
 +   }
 +   if (rr-domains  RADEON_DOMAIN_VRAM) {
 +   rctx-vram += rr-buf-size;
 +   }
 +}
 +
  #endif
 diff --git a/src/gallium/drivers/r600/r600_state.c 
 b/src/gallium/drivers/r600/r600_state.c
 index c0bc2a5..44cd00e 100644
 --- a/src/gallium/drivers/r600/r600_state.c
 +++ b/src/gallium/drivers/r600/r600_state.c

[Mesa-dev] [PATCH] r600g: add cs memory usage accounting and limit it

2013-01-30 Thread j . glisse
From: Jerome Glisse jgli...@redhat.com

We are now seing cs that can go over the vram+gtt size to avoid
failing flush early cs that goes over 70% (gtt+vram) usage. 70%
is use to allow some fragmentation.

Signed-off-by: Jerome Glisse jgli...@redhat.com
---
 src/gallium/drivers/r600/evergreen_state.c|  4 
 src/gallium/drivers/r600/r600.h   |  1 +
 src/gallium/drivers/r600/r600_buffer.c|  1 +
 src/gallium/drivers/r600/r600_hw_context.c| 12 
 src/gallium/drivers/r600/r600_pipe.c  |  3 +++
 src/gallium/drivers/r600/r600_pipe.h  | 21 +
 src/gallium/drivers/r600/r600_state.c |  3 +++
 src/gallium/drivers/r600/r600_state_common.c  | 17 -
 src/gallium/winsys/radeon/drm/radeon_drm_cs.c | 11 +++
 src/gallium/winsys/radeon/drm/radeon_winsys.h | 10 ++
 10 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/src/gallium/drivers/r600/evergreen_state.c 
b/src/gallium/drivers/r600/evergreen_state.c
index be1c427..84f8dce 100644
--- a/src/gallium/drivers/r600/evergreen_state.c
+++ b/src/gallium/drivers/r600/evergreen_state.c
@@ -1668,6 +1668,8 @@ static void evergreen_set_framebuffer_state(struct 
pipe_context *ctx,
surf = (struct r600_surface*)state-cbufs[i];
rtex = (struct r600_texture*)surf-base.texture;
 
+   r600_context_add_resource_size(ctx, state-cbufs[i]-texture);
+
if (!surf-color_initialized) {
evergreen_init_color_surface(rctx, surf);
}
@@ -1699,6 +1701,8 @@ static void evergreen_set_framebuffer_state(struct 
pipe_context *ctx,
if (state-zsbuf) {
surf = (struct r600_surface*)state-zsbuf;
 
+   r600_context_add_resource_size(ctx, state-zsbuf-texture);
+
if (!surf-depth_initialized) {
evergreen_init_depth_surface(rctx, surf);
}
diff --git a/src/gallium/drivers/r600/r600.h b/src/gallium/drivers/r600/r600.h
index a383c90..b9f7d3d 100644
--- a/src/gallium/drivers/r600/r600.h
+++ b/src/gallium/drivers/r600/r600.h
@@ -50,6 +50,7 @@ struct r600_resource {
 
/* Resource state. */
unsigneddomains;
+   uint64_tsize;
 };
 
 #define R600_BLOCK_MAX_BO  32
diff --git a/src/gallium/drivers/r600/r600_buffer.c 
b/src/gallium/drivers/r600/r600_buffer.c
index 6df0d91..92f549a 100644
--- a/src/gallium/drivers/r600/r600_buffer.c
+++ b/src/gallium/drivers/r600/r600_buffer.c
@@ -250,6 +250,7 @@ bool r600_init_resource(struct r600_screen *rscreen,
break;
}
 
+   res-size = size;
res-buf = rscreen-ws-buffer_create(rscreen-ws, size, alignment,
   use_reusable_pool,
   initial_domain);
diff --git a/src/gallium/drivers/r600/r600_hw_context.c 
b/src/gallium/drivers/r600/r600_hw_context.c
index ebafd97..44d3b4d 100644
--- a/src/gallium/drivers/r600/r600_hw_context.c
+++ b/src/gallium/drivers/r600/r600_hw_context.c
@@ -359,6 +359,16 @@ out_err:
 void r600_need_cs_space(struct r600_context *ctx, unsigned num_dw,
boolean count_draw_in)
 {
+   if (!ctx-ws-cs_memory_below_limit(ctx-rings.gfx.cs, ctx-vram, 
ctx-gtt)) {
+   ctx-gtt = 0;
+   ctx-vram = 0;
+   ctx-rings.gfx.flush(ctx, RADEON_FLUSH_ASYNC);
+   return;
+   }
+   /* all will be accounted once relocation are emited */
+   ctx-gtt = 0;
+   ctx-vram = 0;
+
/* The number of dwords we already used in the CS so far. */
num_dw += ctx-rings.gfx.cs-cdw;
 
@@ -784,6 +794,8 @@ void r600_begin_new_cs(struct r600_context *ctx)
 
ctx-pm4_dirty_cdwords = 0;
ctx-flags = 0;
+   ctx-gtt = 0;
+   ctx-vram = 0;
 
/* Begin a new CS. */
r600_emit_command_buffer(ctx-rings.gfx.cs, ctx-start_cs_cmd);
diff --git a/src/gallium/drivers/r600/r600_pipe.c 
b/src/gallium/drivers/r600/r600_pipe.c
index a59578d..cb50cfe 100644
--- a/src/gallium/drivers/r600/r600_pipe.c
+++ b/src/gallium/drivers/r600/r600_pipe.c
@@ -333,6 +333,9 @@ static struct pipe_context *r600_create_context(struct 
pipe_screen *screen, void
rctx-chip_class = rscreen-chip_class;
rctx-keep_tiling_flags = rscreen-info.drm_minor = 12;
 
+   rctx-gtt = 0;
+   rctx-vram = 0;
+
LIST_INITHEAD(rctx-active_nontimer_queries);
LIST_INITHEAD(rctx-dirty);
LIST_INITHEAD(rctx-enable_list);
diff --git a/src/gallium/drivers/r600/r600_pipe.h 
b/src/gallium/drivers/r600/r600_pipe.h
index 3ff42d3..beb4b33 100644
--- a/src/gallium/drivers/r600/r600_pipe.h
+++ b/src/gallium/drivers/r600/r600_pipe.h
@@ -447,6 +447,10 @@ struct r600_context {
unsignedbackend_mask;
unsignedmax_db; /* for OQ */
 
+   

Re: [Mesa-dev] [PATCH] r600g: add cs memory usage accounting and limit it

2013-01-30 Thread Marek Olšák
On Wed, Jan 30, 2013 at 6:14 PM,  j.gli...@gmail.com wrote:
 From: Jerome Glisse jgli...@redhat.com

 We are now seing cs that can go over the vram+gtt size to avoid
 failing flush early cs that goes over 70% (gtt+vram) usage. 70%
 is use to allow some fragmentation.

 Signed-off-by: Jerome Glisse jgli...@redhat.com
 ---
  src/gallium/drivers/r600/evergreen_state.c|  4 
  src/gallium/drivers/r600/r600.h   |  1 +
  src/gallium/drivers/r600/r600_buffer.c|  1 +
  src/gallium/drivers/r600/r600_hw_context.c| 12 
  src/gallium/drivers/r600/r600_pipe.c  |  3 +++
  src/gallium/drivers/r600/r600_pipe.h  | 21 +
  src/gallium/drivers/r600/r600_state.c |  3 +++
  src/gallium/drivers/r600/r600_state_common.c  | 17 -
  src/gallium/winsys/radeon/drm/radeon_drm_cs.c | 11 +++
  src/gallium/winsys/radeon/drm/radeon_winsys.h | 10 ++
  10 files changed, 82 insertions(+), 1 deletion(-)

 diff --git a/src/gallium/drivers/r600/evergreen_state.c 
 b/src/gallium/drivers/r600/evergreen_state.c
 index be1c427..84f8dce 100644
 --- a/src/gallium/drivers/r600/evergreen_state.c
 +++ b/src/gallium/drivers/r600/evergreen_state.c
 @@ -1668,6 +1668,8 @@ static void evergreen_set_framebuffer_state(struct 
 pipe_context *ctx,
 surf = (struct r600_surface*)state-cbufs[i];
 rtex = (struct r600_texture*)surf-base.texture;

 +   r600_context_add_resource_size(ctx, state-cbufs[i]-texture);
 +
 if (!surf-color_initialized) {
 evergreen_init_color_surface(rctx, surf);
 }
 @@ -1699,6 +1701,8 @@ static void evergreen_set_framebuffer_state(struct 
 pipe_context *ctx,
 if (state-zsbuf) {
 surf = (struct r600_surface*)state-zsbuf;

 +   r600_context_add_resource_size(ctx, state-zsbuf-texture);
 +
 if (!surf-depth_initialized) {
 evergreen_init_depth_surface(rctx, surf);
 }
 diff --git a/src/gallium/drivers/r600/r600.h b/src/gallium/drivers/r600/r600.h
 index a383c90..b9f7d3d 100644
 --- a/src/gallium/drivers/r600/r600.h
 +++ b/src/gallium/drivers/r600/r600.h
 @@ -50,6 +50,7 @@ struct r600_resource {

 /* Resource state. */
 unsigneddomains;
 +   uint64_tsize;

Don't add this. Use r600_resource::buf::size instead, which is already
initialized.


  };

  #define R600_BLOCK_MAX_BO  32
 diff --git a/src/gallium/drivers/r600/r600_buffer.c 
 b/src/gallium/drivers/r600/r600_buffer.c
 index 6df0d91..92f549a 100644
 --- a/src/gallium/drivers/r600/r600_buffer.c
 +++ b/src/gallium/drivers/r600/r600_buffer.c
 @@ -250,6 +250,7 @@ bool r600_init_resource(struct r600_screen *rscreen,
 break;
 }

 +   res-size = size;
 res-buf = rscreen-ws-buffer_create(rscreen-ws, size, alignment,
use_reusable_pool,
initial_domain);
 diff --git a/src/gallium/drivers/r600/r600_hw_context.c 
 b/src/gallium/drivers/r600/r600_hw_context.c
 index ebafd97..44d3b4d 100644
 --- a/src/gallium/drivers/r600/r600_hw_context.c
 +++ b/src/gallium/drivers/r600/r600_hw_context.c
 @@ -359,6 +359,16 @@ out_err:
  void r600_need_cs_space(struct r600_context *ctx, unsigned num_dw,
 boolean count_draw_in)
  {
 +   if (!ctx-ws-cs_memory_below_limit(ctx-rings.gfx.cs, ctx-vram, 
 ctx-gtt)) {
 +   ctx-gtt = 0;
 +   ctx-vram = 0;
 +   ctx-rings.gfx.flush(ctx, RADEON_FLUSH_ASYNC);
 +   return;
 +   }
 +   /* all will be accounted once relocation are emited */
 +   ctx-gtt = 0;
 +   ctx-vram = 0;
 +
 /* The number of dwords we already used in the CS so far. */
 num_dw += ctx-rings.gfx.cs-cdw;

 @@ -784,6 +794,8 @@ void r600_begin_new_cs(struct r600_context *ctx)

 ctx-pm4_dirty_cdwords = 0;
 ctx-flags = 0;
 +   ctx-gtt = 0;
 +   ctx-vram = 0;

 /* Begin a new CS. */
 r600_emit_command_buffer(ctx-rings.gfx.cs, ctx-start_cs_cmd);
 diff --git a/src/gallium/drivers/r600/r600_pipe.c 
 b/src/gallium/drivers/r600/r600_pipe.c
 index a59578d..cb50cfe 100644
 --- a/src/gallium/drivers/r600/r600_pipe.c
 +++ b/src/gallium/drivers/r600/r600_pipe.c
 @@ -333,6 +333,9 @@ static struct pipe_context *r600_create_context(struct 
 pipe_screen *screen, void
 rctx-chip_class = rscreen-chip_class;
 rctx-keep_tiling_flags = rscreen-info.drm_minor = 12;

 +   rctx-gtt = 0;
 +   rctx-vram = 0;

There is no reason to initialize anything to 0 in context_create. The
whole context structure is calloc'd.


 +
 LIST_INITHEAD(rctx-active_nontimer_queries);
 LIST_INITHEAD(rctx-dirty);
 LIST_INITHEAD(rctx-enable_list);
 diff --git